Basic Concepts of Object Oriented Programming (Characteristics of Object Oriented Languages)
Basic Concepts of Object Oriented Programming (Characteristics of Object Oriented Languages)
Abstraction:
Abstraction refers to the act of representing essential features which have to be used and required and leaving out the features which are not required. In Object Oriented languages Class is an example of Abstraction and therefore it is called Abstract Data Type (ADT). e.g. if we want to calculate the area of a rectangle then we only require length, width and area as data members of the rectangle class, and there is no need to declare and define any other attribute in the class as given below: class rectangle { private: int length; int width; int area; public: void input() { cin>>length; cin>>width; } void output() { area=length*width; cout<< area is = <<area: } };
Encapsulation:
The Wrapping up of data and functions into a single unit (called class) is known as encapsulation. Data encapsulation is the most important feature of a class. Data in the class are called data members and functions in the class are called member functions of the class. e.g. in the class given below, length, width, area are data members and input() and output() are member functions of the rectangle class. And these all class members work as a unit in the class. e.g. class rectangle { private: int length; int width; int area; public: void input(); void output() };
Inheritance:
The mechanism of deriving a new class from old one is called inheritance. The old class is referred to as the base class or parent class and the new class is called the derived class or child class. The derived class inherits some or all the features of the base class. Inheritance provides the concept of reusability. We can implements all types of inheritance in C++: single inheritance, multiple inheritance, hierarchical inheritance, multilevel inheritance and hybrid inheritance. Example of single inheritance is given below. class person { public: char name[10]; char fname[10]; char address[20]; }; class student : person { public: int enroll_no; char cname[10]; student() { cin<< name; cin<<fname; cin<<address; cin<<enroll_no cin<<cname; } void display(); }; void student :: display() { cin<< name; cin<<fname; cin<<address; cin<<enroll_no cin<<cname; } void main() { student s1, s2; s.display(); } 3
Polymorphism:
Polymorphism is also one of the main features of object oriented programming. Polymorphism means one name, many forms. In C++, there are static polymorphism and dynamic polymorphism. Function overloading and operator overloading are examples of static polymorphism and function overriding is an example of dynamic polymorphism. Example given below illustrates function overloading, one of the type of static polymorphism. class rectangle { private: int length; int width; int area; public: input(); input(int,int); void output(); }; void rectangle::input() { length=5; width=10; } void rectangle::input(int l, int w) { length=l; width=w; } void rectangle::output(void) { area=length*width; cout<<area: } void main() { rectangle r1, r2; r1.input(); r2.input(10,20); r1.output(); r2.output(); } 4
Class:
A class is a way to bind the data and its associated functions together. When defining a class, we are creating a new data type that can be treated like any other built-in data type. The body of a class is enclosed within braces and terminated by a semicolon. The class body contains the declaration of variables and functions. These function and variable are collectively called class members. Generally, a class specification has two parts: 1. class declaration 2. Class function definition Class declaration describes the type and scope of its members. The class function definition describes how the class functions are implemented. class rectangle // class declaration { private: int length; int width; public: void input(); void output(); }; void rectangle::input(void) // class function definition { cin>>length; cin>>width; } void rectangle::output(void) { area=length*width; cout<<area: }
Object:
Once a class has been declared, we can create variables of that type. In C++, the class variables are known as objects or instance. We may declare more than one object in one statement. Class only provides a template and does not create any memory space to the objects and the necessary memory space is allocated to an object when it is declared. 5
Constructor:
A constructor is a special member function whose task is to initialize the object of its class. It is special because its name is the same as the class name. the constructor is invoked whenever an object of its associated class is created. It is called constructor because it construct the values of data members of the class. Constructors must be declared in the public section. They are invoked automatically when the object are created. They do not have any return types, not even void and therefore, and they can not return values. They can not be inherited. Constructors can be of different types: Default constructor Parameterized constructor Copy constructor
Default Constructor:
A constructor that accepts no parameters is called default constructor. If parameterized or default constructor is defined, then it is necessary to define a default constructor. e.g. class rectangle { private: int length; int width; int area; public: rectangle(void); void output(); }; rectangle::rectangle(void) { length=5; width=10; } void rectangle::output(void) { area=length*width; cout<<area: } void main() { rectangle r1, r2; r1.output(); r2.output(); } 7
Parameterized Constructor:
In practice it may be necessary to initialize the various data members of different objects with different values when they are created. C++ permits us to achieve this objective by passing arguments to the constructor when objects are created. The constructor that can take arguments are called parameterized constructor. e.g. class rectangle { private: int length; int width; int area; public: rectangle(int,int); void output(); }; rectangle::rectangle(int l, int w) { length=l; width=w; } void rectangle::output(void) { area=length*width; cout<<area: } void main() { rectangle r1(10,20); r1.output(); }
Copy Constructor:
A constructor through which an object is initialized from another object is called copy constructor. e.g. class rectangle { private: int length; int width; int area; public: rectangle(int,int); rectangle(rectangle &); void output(); }; rectangle::rectangle(int x,int y) { length=x; width=y; } rectangle::rectangle(rectangle &r3) { length=r3.length; width=r3.width; } void rectangle::output(void) { area=length*width; cout<<area: } void main() { rectangle r1(3,5), r2(r1); r1.output(); r2.output(); } 9
Destructor:
A destructor is used to destroy the object that has been created by a constructor. Like a constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde. A Destructor never takes any argument nor does it return any value. It will be invoked implicitly by the compiler upon exit from the program to clean up the storage that is no longer accessible. It is a good practice to define destructor in a program since it release memory space for future use. class rectangle { private: int length; int width; int area; public: rectangle(int,int); ~rectangle(); }; rectangle::rectangle(int x,int y) { length=x; width=y; } void rectangle::output(void) { area=length*width; cout<<area: } rectangle::~rectangle() { cout<< object is destroyed; } void main() { rectangle r1(3,5)); r1.output() };
10
Below given program illustrates the use of static data member: class rectangle { private: int length; int width; int area; static int count; public: void input() { cin>>length; cin>>width; } void output() { cout++; area=length*width; cout<< area of rectangle << count << is = <<area: } }; void main() { rectangle r1, r2, r3; r1.input(); r1.output(); r2.input(); r2.output(); r3.input(); r3.output(); }
11
Below given program illustrates the use of static data member: class rectangle { private: int length; int width; int area; static int count; public: void input() { cin>>length; cin>>width; } void static fcount() { count++; cout>>count; } void output() { cout++; area=length*width; cout<< area of rectangle << fcount() << is = <<area: } }; void main() { rectangle r1, r2, r3; r1.input(); r1.output(); r2.input(); r2.output(); r3.input(); r3.output(); } 12
13
Garbage Collection:
In computer science, garbage collection (GC) is a form of automatic memory management. Garbage collector is a special function or procedure for memory management which attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. Garbage collection was invented by John McCarthy around 1959 to solve problems in Lisp. Garbage collection is often portrayed as the opposite of manual memory management. In manual memory management, objects are found which objects are no longer in use and special functions are called to deallocate them. Many computer languages have inbuilt garbage collector as part of the language specification (e.g., Java, C#, and most scripting languages), but C++ do not have inbuilt garbage collector as part of the language. But we can create a special Garbage collector function or procedure in C++ and can add it in the language.
Friend Function:
In C++, a non member function cannot have access to the private data of a class. But there could be a situation where we would like different classes to share a particular function. In such a situation, C++ introduced a special function called friend function and this function is common to different classes and can access to the private data of these classes. Friend function need not be a member of any of these classes. e.g.
class bus; class car { int length; public: void setvalue(int l) { length=l; } friend void max(car,bus); }; class bus { int length; public: void setvalue(int l) {
14
} };
length=l;
friend void max(car,bus); void max(car c2,bus b2) { if(c2.length>b2.length) cout<<"car is long"; else cout<<"bus is long"; } void main() { clrscr(); car c1; c1.setvalue(12); bus b1; b1.setvalue(15); max(c1,b1); getch(); }
Structure in C++:
A structure is a user defined data type that packs the data of different types together. After definining the structure, we can use it similar to built-in data type, and use it to create variables of its type. But Structure in C has some limitations, it does not encapsulate data and function together, it encapsulates only data. it does not follow most of the OOP features. But in C++, OOP features has been enhanced in Structure. In C++, data and functions both can be encapsulated in Structure and it follow the data hiding and inheritance features. The only difference between a Class and Structure in C++ is: by default all the class members are private and all the Structure members are public. Example: struct rectangle { private: int length; int width; int area; public: void input() { cin>>length; cin>>width; } void output() { 15
area=length*width; cout<< area of rectangle is = <<area: } }; void main() { rectangle r1; r1.input(); r1.output(); }
Types of Inheritance:
The mechanism of deriving a new class from old one is called inheritance. The old class is referred to as the base class or parent class and the new class is called the derived class or child class. The derived class inherits some or all the features of the base class. Inheritance provides the concept of reusability. There are different types of inheritance in C++ as given below: Single level inheritance Multiple level inheritance Hierarchical inheritance Multilevel inheritance Hybrid inheritance
16
class student { private: int enroll; char name[20]; char course[20]; public: void input_stud(); void display_stud(); }; void student::input_stud() { cout<<"enter enroll:"; cin>>enroll; cout<<"enter name:"; cin>>name; cout<<"enter course:"; cin>>course; } void student::display_stud() { cout<<"enroll is:"<<enroll; cout<<"\nname is:"<<name; cout<<"\ncourse is:"<<course; } class exam:public student { private: int msub1; int msub2; int msub3;
17
};
void exam::input_exam() { cout<<"enter marks of subject1:"; cin>>msub1; cout<<"enter marks of subject2:"; cin>>msub2; cout<<"enter marks of subject3:"; cin>>msub3; } void exam::display_exam() { cout<<"\nmarks in subject1 is:"<<msub1; cout<<"\nmarks in subject2 is:"<<msub2; cout<<"\nmarks in subject3 is:"<<msub3; } void main() { clrscr(); exam e1; e1.input_stud(); e1.input_exam(); e1.display_stud(); e1.display_exam(); getch(); }
Access Modifiers:
To implement data hiding in C++, there are three different types of access modifiers: Private: If a data member or member function is declared Private, then it can only be accessed by member functions. Protected: If a data member or member function is declared Protected, then it can be accessed by member functions of its class and derived class. Public: If a data member or member function is declared Public, then it can be accessed by all functions in the program.
18
Accessibility of a data member and member function in the derived class, not only depends on its access modifier; but also on the access mode, by which mode, the base class is derived by the derived class as in the block diagram and table given below:
19
Multilevel inheritance:
The mechanism of deriving a class from another derived class is known as multilevel inheritance.
Example:
class student { protected: char course[20]; char name[20]; int enroll; };
20
class exam:public student { protected: char etype[20]; int msub1; int msub2; int msub3; }; class result:public exam { private: int tmarks; public: void input(); void total(); void display(); }; void result::input() { cout<<"enter enroll:"; cin>>enroll; cout<<"enter name:"; cin>>name; cout<<"enter course:"; cin>>course; cout<<"enter exam type:"; cin>>etype; cout<<"enter marks of subject1:"; cin>>msub1; cout<<"enter marks of subject2:"; cin>>msub2; cout<<"enter marks of subject3:"; cin>>msub3; } void result::total() { tmarks=msub1+msub2+msub3; } void result::display() { cout<<"enroll:"<<enroll; cout<<"\nname:"<<name; cout<<"\ncourse:"<<course; cout<<"\nexam type:"<<etype; cout<<"\n total marks:"<<tmarks; } void main() { clrscr();
21
Multiple inheritance:
The mechanism of deriving a class from two or more classes is known as multiple inheritance.
Example: #include<iostream.h> #include<conio.h> class student { protected: char course[20]; char name[20];
22
};
int enroll;
class exam { protected: char etype[20]; int msub1; int msub2; int msub3; }; class result:public student, public exam { private: int tmarks; public: void input(); void total(); void display(); }; void result::input() { cout<<"enter enroll:"; cin>>enroll; cout<<"enter name:"; cin>>name; cout<<"enter course:"; cin>>course; cout<<"enter exam type:"; cin>>etype; cout<<"enter marks of subject1:"; cin>>msub1; cout<<"enter marks of subject2:"; cin>>msub2; cout<<"enter marks of subject3:"; cin>>msub3; } void result::total() { tmarks=msub1+msub2+msub3; } void result::display() { cout<<"enroll:"<<enroll; cout<<"\nname:"<<name; cout<<"\ncourse:"<<course; cout<<"\nexam type:"<<etype; cout<<"\n total marks:"<<tmarks; } void main()
23
24
} void student::display() { cout<<"enroll:"<<enroll; cout<<"\nname:"<<name; cout<<"\ncourse:"<<course; } class exam { private: char etype[20]; protected: int msub1; int msub2; int msub3; public: }; void input_exam(); void display();
void exam::input_exam() { cout<<"enter exam type:"; cin>>etype; cout<<"enter marks of subject1:"; cin>>msub1; cout<<"enter marks of subject2:"; cin>>msub2; cout<<"enter marks of subject3:"; cin>>msub3; } void exam::display() { cout<<"\nexam type:"<<etype; } class result:public student, public exam { private: int tmarks; public: void total(); void result_display(); }; void result::total() { tmarks=msub1+msub2+msub3; } void result::result_display() {
25
void main() { clrscr(); result r1; r1.input_stud(); r1.input_exam(); r1.total(); r1.student::display(); r1.exam::display(); r1.result_display(); getch(); }
Hierarchical inheritance:
The mechanism of deriving two or more classes from the same class is known as Hierarchical inheritance. The base class will include all the features that are common to the subclasses. A subclass can serve as a base class for the lower level classes and so on.
26
class fee:public student { private: int adminfee; int hostelfee; int transpfee; int totalfee; public: void input(); void total(); void display(); }; void fee::input() { cout<<"enter admission fee:"; cin>>adminfee; cout<<"enter hostel fee:"; cin>>hostelfee; cout<<"enter transport fee:"; cin>>transpfee; } void fee::total() { totalfee=adminfee+hostelfee+transpfee; } void fee::display() { cout<<"total fee:"<<totalfee; } class exam:public student { private: char etype[20]; int msub1; int msub2; int msub3; public: void input(); void display();
};
void exam::input() { cout<<"enter cin>>etype; cout<<"enter cin>>msub1; cout<<"enter cin>>msub2; cout<<"enter cin>>msub3; }
27
void exam::display() { cout<<"\nexam type:"<<enroll; cout<<"\nsubject1:"<<msub1; cout<<"\nsubject2:"<<msub2; cout<<"\nsubject3:"<<msub3; } void main() { clrscr(); exam e1; fee f1; f1.input(); f1.total(); f1.display(); e1.input(); e1.display(); getch(); }
Hybrid inheritance/ ambiguity in hybrid inheritance/ multipath problem in inheritance/ virtual base class:
Sometimes we need to apply two or more types of inheritance to design a program and this type of inheritance is called hybrid inheritance. But in hybrid inheritance, if multiple paths exit between a base class and derived class through different intermediate classes, then derived class inherits the members of the base class more than one time and it results in ambiguity. Here, base class is called indirect base class and intermediate base classes are called direct base classes. To avoid this ambiguity, the indirect base class is made virtual base class and to define base class as virtual, a keyword virtual is appended when extending the direct base classes from the indirect base class as given in the example. In this way, when we define the indirect base class virtual, compiler take care that only one copy of that class is inherited, regardless of how many inherited paths exist between the virtual base class and the derived class.
28
Example:
class student { protected: char course[20]; char name[20]; int enroll; }; class exam:virtual public student { protected: char etype[20]; int msub1; int msub2; int msub3; }; class sports:virtual public student { protected: char sname[20]; int smarks; }; class result:public exam, public sports { private: int tmarks; public: void input(); void total(); void display(); }; void result::input() {
29
cout<<"enter cin>>enroll; cout<<"enter cin>>name; cout<<"enter cin>>course; cout<<"enter cin>>etype; cout<<"enter cin>>msub1; cout<<"enter cin>>msub2; cout<<"enter cin>>msub3; cout<<"enter cin>>sname; cout<<"enter cin>>smarks; }
enroll:"; name:"; course:"; exam type:"; marks of subject1:"; marks of subject2:"; marks of subject3:"; sports name:"; sports marks:";
void result::total() { tmarks=msub1+msub2+msub3+smarks; } void result::display() { cout<<"enroll:"<<enroll; cout<<"\nname:"<<name; cout<<"\ncourse:"<<course; cout<<"\nexam type:"<<etype; cout<<"\ntotal marks:"<<tmarks; }
classes are supplied to the derived class and the derived class supplies these values to its base classes. Example:
class student { private: char name; char course; protected: int enroll; public: student(char na, char co) { name=na; course=co; } void display_stud();
};
class exam:public student { private: char etype; protected: int msub1; int msub2; int msub3; public: { } void display_exam(); exam(char na, char co, char et) :student(na,co) etype=et;
31
}; void exam::display_exam() { cout<<"\exam type:"<<etype; cout<<"\nmarks in subject1:"<<msub1; cout<<"\nmarks in subject2:"<<msub2; cout<<"\nmarks in subject3:"<<msub3; } class result:public exam { private: int tmarks; int mmarks; public: result(int eno, char na, char co, char et,int mm, int m1, int m2, int m3):exam(na,co,et) enroll=eno; mmarks=mm; msub1=m1; msub2=m2; msub3=m3; } void total(); }; void display_result();
void result::total() { tmarks=msub1+msub2+msub3; } void result::display_result() { cout<<"\nMaximum marks:"<<tmarks; cout<<"\ntotal marks:"<<mmarks; } void main() { int eno,ms1,ms2,ms3,mm; char na,co,et; cout<<"enter enroll:"; cin>>eno;
32
cout<<"enter cin>>na; cout<<"enter cin>>co; cout<<"enter cin>>et; cout<<"enter cin>>ms1; cout<<"enter cin>>ms2; cout<<"enter cin>>ms3; cout<<"enter cin>>mm;
name:"; course:"; exam type:"; marks of subject1:"; marks of subject2:"; marks of subject3:"; maximum marks:";
Dynamic polymorphism/ Run time polymorphism/ dynamic binding/ dynamic linking: 33 Function overriding
Function Overloading:
Using a single function name to handle different numbers and different types of arguments to perform different types of tasks is known as function overloading. Example: class rectangle { private: int length; int width; int area; public: input(); input(int,int); void output(); }; void rectangle::input() { length=5; width=10; }
void rectangle::input(int l, int w) { length=l; width=w; } void rectangle::output(void) { area=length*width; cout<<area: } void main() { rectangle r1, r2; r1.input(); r2.input(10,20); 34
r1.output(); r2.output(); }
Operator overloading:
The process of making an operator to exhibits different behaviours in different instances is known as operator overloading.
class rectangle { private: int length; int width; public: void input(); rectangle operator+(rectangle); void display(); }; void rectangle::input() { cout<<"enter length:"; cin>>length; cout<<"enter width:"; cin>>width; }
rectangle rectangle::operator+(rectangle r4) { rectangle r5; r5.length=length+r4.length; r5.width=width+r4.width; return(r5); } void rectangle::display() { cout<<"length:"<<length; cout<<"\nwidth:"<<width; } void main() { clrscr();
35
// r3=r1.operator+(r2);
Function overriding:
If there are two functions with the same name in the base class and derived class and even the address of derived class is assigned to the pointer of base class; it executes the function of base class. But by declaring the base class function as virtual, pointer to base class executes the function of derived class. And in this way base class pointer behaves differently at different situation and applies the concept of polymorphism. This is also called run time or dynamic polymorphism because when a function made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer, rather than the type of the pointer.
class base { public: virtual void display(); virtual void show(); };
36
void base::display() { cout<<"\nbase class display() function"; } void base::show() { cout<<"\nbase class show() function"; } class derived: public base { public: void display(); void show(); }; void derived::display() { cout<<"\nderived class display() function"; } void derived::show() { cout<<"\nderived class show() function"; }
void main() { clrscr(); base *b; derived d; b=&d; b->display(); b->show(); getch(); }
37
Template:
Template is one of the features added to C++ recently. It is a new concept which enables us to define generic classes and functions and thus provides support for generic programming. A template is defined with a parameter that would be replaced by a specific data type at the time of actual use of the class or function.
Template Class:
The class template definition is very similar to an ordinary class definition except the prefix template<class T>. This prefix tells the compiler that we are going to declare a template and use T as a data type in the declaration. This T would be replaced by a specific data type at the time of actual use.
Example: template<class T>
38
class sinterest { T pvalue; T time; T rate; T si; public: sinterest(T pv,T tt,T r) { pvalue=pv; time=tt; rate=r; } void display() { si=pvalue*time*rate/100; cout<<"simple interest is:"<<si; } }; void main() { sinterest <int> s1(1000,8,2); sinterest <float> s2(100000.00,8.5,5.0); s1.display(); s2.display(); } getch();
Template Function:
The function template syntax is similar to that of the class template. The function template definition also prefix template<class T>. this prefix tells the compiler that we are going to declare a template and use T as a data type in the declaration. This T would be replaced by a specific data type at the time of actual use.
template<class T> void cal_inter(T pv,T tt,T r) { T pvalue=pv; T time=tt; T rate=r; T si=pvalue*time*rate/100; } cout<<"\nsimple interest is:"<<si;
39
40