object oriented design using procedural programming?

Started by
22 comments, last by GameDev.net 18 years, 1 month ago
The classic OOP-in-a-procedural-language example in plain C is the humble FILE *. A FILE * is an object and there are functions to create a FILE * (fopen), destroy a FILE * (fclose) and perform operations on a FILE * (fread, fseek, etc.).

Σnigma
Advertisement
okay here's my new class design heirarchy.

root | |----------------------------------------- |                    |                   |write_type       delete_type           list_type |                    |                   |write i/o        delete file              |and then           and then               |goto list_type     goto list_type         |                                          |                          -----------------------------------                          |                                 |        from write/delete or a none action              search action                          |                                 |                     generate_output                      filter                          |                                 |             parse my language code                 goto generate_output                          |                     group by similar functions                   such as GROUPA=add, subtract                           GROUPB=switch, if                           GROUPC=for, while, while-do loops


is this object oriented programming?
even though it's not, I know this type of heirarchy would eliminate much of the repetitive codes.
It sounds like logical objects for you are

1) Your list.

2) Actions, probably derived from a base 'Action'

3) A list of Actions you have performed on the 'List'

Then, instead of doing 'if type == 1 || type == 2...', you would simply do something like "action->execute();", where 'action' is derived from your base Action class in any manner you need.
well, I'm very unsure,
I'm getting my oop cherry popped today :D
first time writing oop code.
does this seem reasonable?
and if it can be done better, please advice me!!

Thanks anonymous poster!
but I have no idea what you mean by :
Quote:where 'action' is derived from your base Action class in any manner you need.

I only read about oop for 3 days, I don't know much.

but looks like i'm sort of on the right track, i'll keep rereading your post and try to understand what you mean there.
This is only a bare bones outline:

#include <vector>#include <string>class List {   private:      std::vector<std::string> thingsC;   public:      void add(const std::string & str) {         thingsC.push_back(str);         }      void remove(unsigned int index) {         if (index < thingsC.size()) {            std::vector<std::string>::iterator it = thingsC.begin()+index;            thingsC.erase(it);            }         }      void change(int index, std::string newString) {         if (index < thingsC.size()) {            std::vector<std::string>::iterator it = thingsC.begin()+index;            (*it) = newString;            }         }   };List * globalList;class Action {   public:      virtual void execute() { }   };class AddAction : public Action {   private:      std::string strC;   public:      AddAction(const std::string & str) : strC(str) { }      virtual void execute() {         globalList->add(strC);         }   };int main(int argc, char* argv[]) {   List list;   globalList = &list;   //The following is the action:   AddAction actionA("My First String");   //But the 'list' won't contain the string until you do the following:   actionA.execute();   return 0;   }


Hope that helps. (The 'remove' and 'change' functions are kinda pointless as this stands...)
PS - A better name for 'AddAction' is 'AddToList'

(ie class AddToList : public Action { ...)

Avoiding redundancy in your names can sometimes keep your brain from developing a thick glaze...
Quote:Original post by Anonymous Poster
This is only a bare bones outline:

*** Source Snippet Removed ***

Hope that helps. (The 'remove' and 'change' functions are kinda pointless as this stands...)


That was really kind and helpful of you.
Thank you very much, you really didn't have to write all that down.
and just out of curiosity, how long did it take you to write this code?
Quote:Original post by Tradone
...and just out of curiosity, how long did it take you to write this code?


About three minutes? I wasn't really paying attention to the time, but as you become more familiar with OOP, you will find that something like that doesn't take much time at all.
okay, the syntax is miserable..
but this is what I had in mind..
I can edit the syntax later. sorry i'm still learning!
and also i'm not good at using pointers.

main(){	const get parameters	cosnt get password	root new_object( env_var, cookies );}shenu::root( ){	public root::generate_error;	const skin=get.skin;	if ( no db )		generate_error	else ( db )		what are the actions?		if ( action == none || action == search || action == input_form || action == list || action == cont_form )			initialize.list()		else if ( action == write, modify, reply modify reply )			initialize.write()		else if ( action = delete, delete modify )			initialize.delete()}shenu::initialize_list(){	get list_of_cfg	get list_of_path	const skin_file;	there are only 3 types	cont, input, list	generate_html( skin_file )}shenu::initialize_delete(){	delete	initialize_list();}shenu::initialize_write(){	file i/o	serialize	initialize_list();}shenu::generate_html_except_body( filename ){	if( require_cookies_authentication == 1){            start parsing.	}	else{		generate_error ( permission_denied );	}}shenu::generate_error( type ){      switch depending on type.}


you don't have to edit this for me.
or correct the syntax.
I need to learn the syntax as I fix up this big mess!!
but does this structure seem alright?
Quote:Original post by Anonymous Poster
Quote:Original post by Tradone
...and just out of curiosity, how long did it take you to write this code?


About three minutes? I wasn't really paying attention to the time, but as you become more familiar with OOP, you will find that something like that doesn't take much time at all.


I don't think I can ever be able to do that.

This topic is closed to new replies.

Advertisement