- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
About the Object-Oriented Programming Language C++
Conventional or traditional programming method usually deals with basic data types such as integer or character and is quite familiar with the common user. But if the volume of data involved in a problem is more, it becomes difficult to program using conventional programming. In such case, data is organized to form a class (like structure in C) of user-defined type and programming is performed on that data type. We use the term ‘object’ instead of ‘variables’ for the data belonging to the class and so the name object-oriented programming.
Example:
class emp { char name[20]; int empcode; int salary; };
The class ‘emp’ has 3 basic data type members and the statement
emp A; creates an object A of the class type emp.
C++ Programming Language
Several enhancements made to C programming language gave rise to C++. An intermediate level language combining both the features of a low level and high level language is C++. It was developed by the computer scientist Bjarne Stroustrup at Bell Labs. Being a popular programming language, its applications cover a variety of domains such as system software, embedded systems, device drivers, entertainment applications like video games etc. The added features made it easier to do programming involving objects of a class type.
1. Data encapsulation
Data described within a class (data members) is private and can be accessed only by the functions declared inside the class called member functions. Functions declared outside the class have no access to the data members of the class. This property is termed as data hiding which is the core part of object-oriented programming.
Example:
class student { char name[]; int rollno; public: void getdata(); void putdata(); }; student A; void getdetails();
Object A belongs to class ‘student’ and it can access the data members only through the member functions declared in the public section. A.getdata() invoke the getdata() function and reads the data namely name and rollno of the object A but, the function getdetails() has no access to the data members as it is declared outside the scope of the class student. This binding of data and functions into a single class-type variable is referred to as data encapsulation.
2. Operator overloading
This concept lets operations to do with class data types as we do with basic data types. Operators are overloaded using a function by passing parameters to perform the operations. Member functions or friend functions can overload an operator to do basic operations with the objects of the class to which they belong to.
Example:
Class complex { Int real; Int imag; public: void getdata(); void putdata(); complex operator +(int); }A,B;
This class ‘complex’ has two data members real and imag. The first two functions read and write the object details whereas the third one is the overloaded binary operator member function that adds two objects of the class complex. This function can be defined as:
complex operator+(int) { complex C; C.real=real+B.real; C.imag=imag+B.imag; return(C); }
The statement C=A+B;
invokes this function and the object A is passed implicitly, but B explicitly. The sum of the values gets stored in C, the result. This is an instance of complex numbers addition.
3. Inheritance
This enhanced feature of C++ allows program development to advanced modes. Like a child born to its parent, new class also can be derived from an existing class. This new class may inherit the characteristics of the existing class which introduces the concept of code reusability. The existing class is called base class and new class becomes the derived class. The base class members are accessible to the derived class depending upon the visibility mode it inherits. The derived class can add data members or member functions to it and can override the base class.
The derived class inherits only public and protected members and not private members. Also usually, new classes are inherited in public or protected mode, rather than in private mode.
How base class members are inherited in public mode
Inheritance of base class members in protected mode
The general form of a derived class is:
class [derived class name]:[visibility mode] [base class name]
Consider a program:
class time { Int hr,min,sec; public: void gettime(); }; class period:public time { Int dur; public: int getdur(); }A;
In this program, ‘time’ is the base class and ‘period’ derived class. The statements A.gettime() and A.getdur() would invoke the member functions of both the classes as it is publicly inherited.
Various types of inheritance exist depending upon the problem type. They are multiple inheritance, hierarchical inheritance and hybrid inheritance.
4. Polymorphism
This is the ability of a function to exhibit unique behavior when defined in each of the derived classes and invoked through a pointer to an object of the base class. The concept of virtual functions makes this property visible in C++.
Consider this example:
class Org { int basic; public: void setvalue(int x) { basic=x; } virtual int salary() { return(0); } }; class Manager: public Org { public: int salary() { return(basic+basic*0.9); } }; class Accountant: public Org { public: int salary() { return(basic+basic*0.3); } };
Let Organization be the base class and Manager, Accountant as the derived classes. The amount of salary given out to the manager and accountant varies and this calculation can be done by using polymorphism property.
Now, if A, B, and C are the objects of Org, Manager and Accountant classes respectively, the statements
Org *x=&A;
Org *y=&B;
Org *z=&C;
would declare pointers of base class(Org) objects to point to these objects A, B and C in the main function. The statements x->setvalue(10000), y->setvalue(6000) and z->setvalue(2000) would set the ‘basic’ value of these pointer objects. Then the statements
x->salary(), y->salary() and z->salary() would print out the salaries of Org, Manager and Accountant classes separately. This property of executing the functions defined in different derived classes with the same name is called polymorphism in C++. This is accomplished by declaring it as a virtual function in the base class. If it were not declared as virtual, the base class function only would have got executed all the time when invoked using a pointer to an object of the base class.
In a class hierarchy, polymorphism plays an important role in solving various problems that have to be dealt within derived classes.
Please vote!
Is C++ easy for you?
- The concept of operator overloading in C++
Operator overloading concept gives a special meaning to an operator such as ‘+’ or ‘-‘. Addition or subtraction cannot be directly performed with objects as we do with basic data types. Like overloading functions, operators also can be overloaded for - About C Programming; An Overview
Why C has gained this much popularity? It is its portability that differentiates it from other programming languages. A program written in C in one computer system can be transferred to another with minimal changes. This is why the name ‘C’ is most h
© 2013 Radhika Sreekanth