How to write OO?

Pages: 12
Hi,

I'm a C programmer.

I've taken a C++ course and learned the syntax of C++ Classes, inheritance, polymorphism, etc.

I need to write a tool which parses text files and as a result edit other text files, in a project.

I'll write the tool in C++.

How do I integrate Classes and inheritance into my tool, and not write C++ in the C-way?

I'd really like to incorporate Inheritance into my tool, as this was a very difficult subject for me to understand conceptually.

It is very important to me to write in in C++ OO way.

Thank you very much!
Last edited on
Depending on what you need to do, you might not need to create any new C++ classes. Generally you will only need to use existing ones such as std::ifstream, std::ofstream, std::string, std::vector, etc.

Keep in mind, C++ is not C "with classes":
http://www.LB-Stuff.com/cc
Last edited on
Hi,
Thank you.

What do you mean please by not need to create any new Class?

Are you saying that I may write all in static methods, in the same class of main method?

I'd really love to use inheritance to better practice it.
I think what he means is that you can, depending on your code, not actually need to create new classes- the ones that already exist in the standard library usually suffice. Not every situation calls for OO-programming, after all.
jared181 wrote:
Are you saying that I may write all in static methods, in the same class of main method?
I thought you said your background was in C? This is the kind of thing a Java developer would say. The main function is not associated with any class, it is in the global scope.
jared181 wrote:
I'd really love to use inheritance to better practice it.
Inheritance is not suited to every problem in existence. Your problem might be better solved without it (though it is still easier to use C++ than C in many cases). You have not given sufficient information for us to help you make that decision.
Last edited on
Hi,
Thank you very much LB and Isplil :)

LB

LB wrote:
I thought you said your background was in C? This is the kind of thing a Java developer would say. The main function is not associated with any class, it is in the global scope.


In C++, all function are associated with Classes, aren't they?

Also the main function.

LB wrote:
Inheritance is not suited to every problem in existence. Your problem might be better solved without it (though it is still easier to use C++ than C in many cases). You have not given sufficient information for us to help you make that decision.


Oh Please let me detail on the Tool I need to build.

In our project, the Functions and Variables are mapped to different Memory Banks.

After each linkage, we get an output file which shows which functions / data are located at each Memory Bank, and how full that Memory Bank is.

When a certain Memory Bank gets full, I'd like the script to edit the source files of the project, in order to take some functions / variables from the fully occupied Memory Bank and transfer them to less Occupied Memory Bank.

For that, the Script will firstly have to go over the linkage output file, and then go edit the relevant source file.

I'd really like to embed OO in this tool (and inheritance), to enhance my C++ capabilities.

But, since I'm not experienced in OO programming, I'd love to have a starting point and general view point from an experienced C++ programmer.
Last edited on
jared181 wrote:
In C++, all function are associated with Classes, aren't they?

No. Functions can, and often times are, standalone. C++ provides classes if you need them, but does not force them onto you.

The rest of what you said makes no sense. Maybe what you were trying to say was lost in translation?
Hi,
Thank you.

I thought that in good C++ Programming, every function is part of a class.


The rest of what you said makes no sense. Maybe what you were trying to say was lost in translation?


What doesnt make sense to you please?
if you want to deal with Memory Bank then create a class

1
2
3
CMemoryBank
{
};


Now consider what traits such a memory bank has. For instance Occupied:

1
2
3
4
5
6
7
8
CMemoryBank
{
  int m_Occupied;

  CMemoryBank() : m_Occupied(0) // This initialize an empty memory bank
  {
  }
};


Now you may continue to equip with an Allocate/Dealocate function. You may use it like so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
  CMemoryBank memory_bank[10];
  int cur_idx = 0;
...
  if(! memory_bank[cur_idx].Allocate(...))
  {
    ++cur_idx;
    memory_bank[cur_idx].Allocate(...);
...
  }

  return 0;
}
What doesnt make sense to you please?

It sounds like you're wanting to edit source files at runtime in order to move objects in memory around. That makes no sense.
coder777

Thank you very much.

You have given me great start!

Do you have an idea for inheritance?


ResidentBiscuit

I'm not editing source files at run time, but after linkage.

After the source files are written, compilation and linkage take place.

Then, I'll run my script in case one of the Memory Banks is full while others are free.
I'm not editing source files at run time, but after linkage.

if you are editing after linkage and not recompiling then im pretty sure the only step left in c++ would be runtime.
Did you know that there is a bridge above this thread?

-Albatross
Last edited on
no
Hi,

The scripts edit the source files (for re-organizing some of the Functions / Variables) after linkage is done.

Then, of course, another Compilation + Linkage takes place, for generating the final BIN image.

coder777 suggested a wonderful idea to create a MemoryBank Class.

Do you have an idea how I could integrate inheritance into the tool?
What is your obsession with abusing object-oriented concepts when you do not necessarily need them?
Inheritance might come into play when data is involved:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class CMemoryBank
{
  class CData
  {
    virtual bool WriteTo(CMemoryBank &mem) = 0;
    virtual bool ReadFrom(CMemoryBank &mem) = 0;
  };
};

...

class CSomeData : public CMemoryBank::CData
{
  int m_Something;

  virtual bool WriteTo(CMemoryBank &mem)
  {
    void *p = mem.Allocate(sizeof(m_Something));
    bool is_ok = (p != nullptr);
    if(is_ok)
      memcpy(p, &m_Something, sizeof(m_Something));
    return is_ok;
  }
};
Note that it is just the idea not usable code (basically pseudocode)
Last edited on
I smell troll. I'm out.
Hi,
Thank you very much coder777

I love to read your wise posts.

LB , ResidentBiscuit

Let me please explain.

This is the 1st tool i'm developing in C++.

I'd really like to incorporate everything I learned into this tool, even if it's "forced", because that would be the only way for me to keep exercising what I learned.

From my past experience, I know that If I won't use for example inheritance, I'd simply forget about it.

You see my motivation?

Now, since I'm not experienced with c++, I still don't have the intuition as for when to use inheritance or other C++ topics, and that is where my questions are coming from.

Thank you.
You should never force yourself to use something just because you want to - that's a bad idea and leads to poor design.

Instead, why not search for exercises to solve that require using the features of C++ you want to learn?
Pages: 12