Add to Technorati Favorites

Classes & Objects

Last time, we learnt some object-oriented concepts. Now, is the time to see how a class is declared, what is the general structure of a class, how objects are instantiated and used. Let us start with class declaration and its structure.
class strofaclass
// initializing
// clean-up
// accessors
// modifiers
// assignment
// operators
// friends
// representation
// utilities
A class is declared using the keyword class. We saw yesterday that a class encapsulates data members and member functions. Usually, data members are private and member functions are public. In class, everything is private by default whereas in a structure, everything is public by default. Member functions can be categorized into 7 types:
1. Constructors for initialization.
2. Destructors for clean-up.
3. Accessor member functions that provides you access to private data members. Also called 'getters'.
4. Modifier member functions through which you set values of data members. Also called 'setters'.
5. Assignment member functions in which you do some processing and then assign values to data members.
6. operators are for overloading built-in C++ operators for giving similar meaning to user-defined data types.
7. Friend (class or function)declarations that can use private parts of this class.
You can either define member functions inside a class or outside it using scope resolution (::) operator.
Once you have defined your class, you can instantiate its objects in a similar way as you declare variables of built-in types. You may use any of the following syntax:
class_name obj1, obj2;
class_name obj1(arg_list);
class_name *pObj1;
pObj1 = new class_name;
class_name objarray[10];
Using objects is similar to using structures in 'C'. You can use objects using following syntax:
obj1.age = 5;
pObj1->height = 5.8;
If some terms seem new then don't worry, we will cover them in subsequent posts.


chinu said...

good yaar
its much better than books
i like it.

Zens said...

Thanks Chinu. I am posting daily on learning C++ naturally. So keep checking it and give feedbacks and suggestions so that I can improve.