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.
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;
pObj1 = new class_name;
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.
Download Full screen Turbo C++ For Windows
2 years ago