Hello, i have been reading the chapter in the book about classes and i have learned everything about them the only question i have now is that wut do you use classes for?

Member Avatar for iamthwee

Hello, i have been reading the chapter in the book about classes and i have learned everything about them the only question i have now is that wut do you use classes for?

Have you ever used the struct keyword to create your own simple type combining several variables together? That's an extremely simple example of a class.

use just use any class name preceeded by class keyword and put the data and function members as public,private and protected as needed and enclose all of them in braces and DON't forget to end up with semicolon.And class definition goes to .h file.

Have you ever used the struct keyword to create your own simple type combining several variables together? That's an extremely simple example of a class.

with the plus that they can have their own intern functions... they make your variable management even easier...

Classes are a way to localize all the state (data) and services (typically member functions) associated with a cohesive concept. The main idea is to organize things so that when changes to the concept or abstraction occur (as is inevitable), it will be possible to go to one place to make the necessary modifications. We have seen examples in the procedural world where a simple change required modifying dozens of source code files. The people responsible for this failure didn't "get it"; they may have been using C++, but they weren't using it properly. It's easy for beginners, no matter how much experience they have, to fall into this trap.

yeah i use struct in C but not in C++ though it supports for struct.

yeah i use struct in C but not in C++ though it supports for struct.

In which case, you're probably aware of how handy it is to be able to group data together in a struct with your own data type. The idea of a class extends the concept of organising data in this way by throwing functions into the mix.

for example, if you've ever created a struct in a 'C' program, you've probably ended up with something like this in a header file

typedef struct
{
    int i;
    char* str;
    int size;
} my_type;

void do_something(my_type*);
void something_else(my_type*);
int another_function(my_type*);

The functions which accept a pointer to my_type are all designed to do something with a my_type object, So why not strengthen that relationship? - this is what classes do!

With a class, you could re-write that bit of C code like this

class my_type
{
    int i;
    char* str;
    int size;
public:
    void do_something();
    void something_else();
    int another_function();
};

The benefit here is that you no longer need to pass a my_type pointer to any of those functions, because each function is directly associated with my_type, and has one-to-one access with other data members of my_type.

classes bring up huge numbers of design issues, which opens up a whole new world of designing programs. These issues are best examined from a high-level OOP/OO-Design perspective (And i strongly reccomend reading up on OO Design) - which introduces ideas that may be unfamiliar to someone from a procedural programming background, but you can probably see where the foundations of OOP come from - mainly, a desire for discipline when it comes to code organisation.

commented: Good ~SpS +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.