What is Object Oriented Programming in C++


Object oriented programming starts from a ‘bottom up’ principal with emphasis on the data structure with in the program rather than the control flow of the program . during the design process, the language is extended by defining new data types and new operations that directly relate to the need of the application. These objects are fundamental to any OOP language. As they combined and build upon, the application develops. With OOP the idea can be translated directly into a computer program OOP allow us to construct models of the problem and the solution that bridge the way people think, with the way OOP are constructed.

Procedural programming follows top-down approach. Unlike procedural programming, OOP emphasizes on data. OOP attempts to fit the language to the problem. It goes from lower level of organization, such as classes, to a higher level, such as program design. Object based programming is the style of programming that incorporates all of object-based programming features along with two additional features namely inheritance and dynamic binding.

Object oriented programming can, therefore be characterized by the following statements:

OOP=object-based features + inheritance + dynamic binding

Some of the language that support these features are C++, smalltalk, object Pasc

  al.etc

1.C++ Classes

 The word “class”   is a fundamental and powerful keyword in C++.A class is a user defined data type which holds both data and functions.The internal data of a class is called member data (or data member) and the functions are called  member functions The member functions manipulate the internal data of a class. The member data of a class should not be addressed outside a member function.

A class is a user defined data type which  consist of two sections, a private and a protected section that holds data and a public section that holds the interface operations.The syntax of the class construct is:

Class user_defined_name{

Private:

Data_type members

Implementation operations

List of friend functions

List of friend functions

Public:

Data type members

Implementation operations

Protected:

Data_type operations

Implementation operations

};

class user_defined_name variable1,variable2..variable n;

For  example:

(1)   A class date is defined  as day,month and year of a member data variable without any method or any member function.

Class date{

Private:

int day;

int month;

int year;

};

(2)   The student’ particulars such as roll no,age,sex,height and weight can be grouped as:

Class student{

Private:

long in rollno;

int age;

char sex;

float height;

float weight;

public:

void get();              //member function

void dis();            //member function

};// end of the definition

The keyword private ,protected and public are used to specify the three levels of access protection for hiding data and function members internal to the class.

2.Object in c++:

The variables of  a class are called objects or instances of a class.object are often used to model real word entities that we find in our daily life.object represent the class.An object is work only for those class in which object is created  so,It is called instance of a class.Object is real time entities. it occupy some space in memory.

for example:

Class date{

Private:

int day;

int month;

int year;

};

class date today;// today is object of a class

create user define class and object in c++

#include<iostream.h>

#include<conio.h>

class abc                // class name

{

public:                //access modifier

void display()          //data function

{

cout<<“display”;

}};

void main()

{

abc a1;                 //object of class is created with class name(abc)

clrscr();

a1.display();           //call function with object and (.)operator

getch();

}

output:

display

 

Difference between class and object

class Object
  1. A class is a collection of similar type of object is called a class.

 

  1. Class is a specific or logical entity.

 

  1. Class does not consume space in memory.

 

  1. Class made with object.

 

  1. Class is a set of object that share common attitude’s and behavior’s.

 

  1. Furniture is an example of class which has table chair and sofa.
1.Data and function that operate on that        data are binded together to from an object

 

2.Object are real word entities

 

3.Object occupy some space in memory.

 

4. Object represent class.

 

5. Object is an instance a class.

 

 

6.  Table,chair,sofa are the example of object.

3.Abstraction:

In this concept we can say that abstrace the data or function from the class which is relevant to user application and without knowing how they are operate for e.g if in the class there are ten variable and five function are declare then in main if user access only two variable and two function  then it create no problem because the user can access only those function or variable which are useful for user.This is due to data abstraction.

for example:

class base

{

public:

void get()

{

cout<<“base class”;

}};

class Aclass: public base

{

public:

void A()

{

cout<<“Aclass class”;

}};

class Bclass: public base

{

public:

void B()

{

cout<<“Bclass class”;

}};

class Cclass: public base

{

public:

void C()

{

cout<<“Cclass class”;

}};

Void main()

{

Aclass objA1;

objA1.A();

Bclass objB1;

objB1.B();

Cclass objC1;

objC1.C();

}

There are three classes-Aclass,Bclass,Cclass-each class of which is drived from the class base.The main() function declares three objects of each of these three classes.However, it does not declare any object of the class base.This class is a general class whose purpose is to serve as a base class for thr other three.Classes used only for the purpose of deriving other classes from them are called as abstract classes.Abstract classes are not used to define an object(instance) of these classes,they are simply there to serveas base class.

4.Encapsulation:

The wrapping of data and function into a single unit known as encapsulation.most important feature of a class is encapsulation. In c++ ,we can implement the concept of encapsulation using class.Data  and function of a class may be private or public.Private data function of a class can not be accessed out side.The public class and public data or function of a class can be accessed from anywhere. Data of a class is private  so that it cannot be accessed by function outside the class and reduce the misuse of the data.

Example

#include<iostream.h>

#include<conio.h>

class abc

{

protected:

void disp()

{

cout<<“hello”;

}};

class xyz : public abc

{

public:

void show()

{

cout<<“OK”;

xyz ob2;

ob2.disp();

}};

class def:public abc

{

public:

void get()

{

xyz ob3;

ob3.show();

cout<<“bye bye”;

}};

void main()

{

def  ob1;

ob1.get();

getch();

}

 

5.Polymorphism:

poly morphism is very powerful concept. It allow to create the flexible application.This word polymorphism is drive from two Greek words.Poly which means many and morphs which means from. So,polymorphism means the ability take many forms.

Polymorphism can be defined one interface multiple methods,which mean that one interface can be used to perform different but retrieve activities.

 

 

 

 

Type of Polymorphism:

There are two types of polymorphism.

1.) Compile Time or Static polymorphism

2.) Run Time or Dynamic polymorphism

1.) Compile Time or Static polymorphism:

In compile time  polymorphism ,Static binding is performed.In static binding the compiler makes regrading selection of appropriate function to be called in response to function call at the compile time.

It is also called static polymorphism.In this compiler take the decision for selecting function at the compile time.It also known as “early binding”.It has advantage that it take less memory.Compile time polymorphism can be implement using “Overloading“.

Overloading:

In c++ overloading is reuse of the same function and operation.Overloading can be implemented using function and operator. so, we can say that it has two types

1. Function overloading

2. Operator overloading

Function  Overloading:

In function overloading  method name are same ,but we pass the argument different type for example

/*Program to use function overloading*/

#include<iostream.h>

#include<conio.h>

class abc

{

public:

void sum(int a,int b) // method name

{

int c;

c=a+b;

cout<<”c=”<<c<<endl;

}

void sum(double a,double b) //same method name but pass different type arguments

{

double c;

c=a+b;

cout<<”c=”<<c<<endl;

}

void sum(char a,char b)  //same method name but pass different type arguments

{

char c;

c=a+b;

cout<<”c=”<<c;

}

};

void main()

{

clrscr();

abc a1;

a1.sum(5,4);

a1.sum(5.2,6.4);

a1.sum(‘v’,’k’);

getch();

}

/* output:

c=9

c=11.6

c=v*/

In this program we create the method name “void sum” but pass the argument with different data typee.g int ,Double, char.

Advantages of function overloading is:

1.Use same function name for same operation.

2. Helps to understand and debug easily.

3. Easy Maintainability of the code.

Operator Overloading:

operator overloading is the another example of C++ polymorphism.It is the most Challenging and exciting feature of C++. The  main advantages of using operator overloading in a program are that it is much easier to read and debug. operator overloading is also called as operational polymorphism.Polymorphsim facilitates creating multiple definitions for operators and functions.in operator overloading the actual instruction of the operation are written in a special member function defined with the keyword operator followed by the operator itself which is to be overloaded.such a member function is called as operator function.e.g

class  abc

{

private:

int x,y;

public:

void operator++(void);

};

void abc::operator++(void)

{

x++;

y++;

}

The above example shows how operator overloading is implemented.It overloads the increment operator(++)to perform a certain operation on objects of class abc.The function operator++() has to be defined just like any other member function .it contains the instructions to be carried out on the member variables of the operand objecr.All the C++ operators can be overloaded.Each category of operators are covered below,separately.

Overloading Unary Arithmetic Operators:

Unary operator have only one operand.There are three unary operators-The increment operator(++),The decrementy operator(-) and the unary minus operator.e.g

class Counter

{

private:

int count;

public:

Counter(void)

{

count=0;

}

void operator++(void)

{

++count;

}};

void main()

{

Counter c11;

c11++;     //increments count to 1.

++c11;     //increments count to 2.

}

The above example contains a class Counter.an object of this class can be declared with the statement(Counter c11;).In main() the increment operator is applied to a specific object.The function itself does not take any arguments.It increments the data member count.Similarly,a function to decrement the counter object can also be coded in the class definition as:

void operator- (void)

{

-count;

}

and invoke with the statement

-c11;    or           c11-;

Overloading Binary Arithmetic Operators:

Binary operators are operators with two operands.They can also be overloaded just like unary operators.e.g

c1=c2+c3;

if c1,c2 and c3 are objects of class than the compiler will require an operator function for the plus symbol.The following example illustrates the addtion of two objects using the same.

Example:

#include<string.h>

#include<iostream.h>

#include<conio.h>

class  String

{

private:

char s[100];

public:

String(void)    //Constructor with no argument

{

s[0]=0;

}

String(char str[])        //Constructor with no argument

{

strcpy(s,str);

}

void Putstr(void)      // function to print the String

{

cout<<s;

}

String operator+(String s2)                //operator function

{

String temp;                   //create a temporary object

strcpy(temp.s,s);            //copy this string

strcat(temp.s,s2.s);        //add the argument string

return(temp);                  //return the temporary object

}}

void main()

{

String s1=”welcome,”;

String s2=”to the world of c++”;

String s3;

cout<<“\ns1=”;

s1.PutStr();

cout<<“\ns2=”;

s2.PutStr();

s3=s1+s2;

cout<<“\ns3=”;

s3.PutStr();

}

In the above example,the statement to concatenate the two strings

s3=s1+s2;

gets converted ,by the compiler,to the following:

s3=s1.operator+(s2);

Thus the operator can access two objects-first one is the object which invokes the function,i.e is s1 and the second is the argument object s2.The definition of the operator function in the class definition contains a String object as its argument .The operator function concatenates the strings of the two objects using the string handling functions into the string of a newly created temporary object temp and returns it.Similarly,other binary arithmatic operators can overloaded like the addition operator described above.

6.Inheritance:

Inheritance is an important feature of object oriented .It is a process of driving a new class from the exiting class without modifying it .The new class is called “D rived class” or “sub class” or child class” and the existing class is called the “Base class” or “super class” and “parent class” the drived class inherit all the feature of the base class and you can also add some new feature into it.They are five type.

1.) Single Inheritance

2.) Multiple Inheritance

3.) Multilevel Inheritance

4.) Hierarchical   Inheritance

5.) Hybrid Inheritance

1.) Single Inheritance:

In which one “parent class” and one “child class“,mean one “base class” and one “drived class”.

 

 

 

 

/* program of single inheritance*/

#include<iostream.h>

#include<conio.h>

class a   // Parent Class

{

public:

void dis()

{

cout<<”dis”;

}};

class b:public a   //Child class b

{

public:

void show()

{

cout<<”show”;

}};

void main()

{

clrscr();

class b a1;

a1.dis();

a1.show();

getch();

}

/* ouput:

dis

show*/

2.)Multiple Inheritance:

In Multiple inheritance one Grand parent class and one Parent class ,one child class inherit the properties of parent class and also inherit the Grand parent class for example

 

 

 

 

 

 

 

/* Program of multiple inheritance*/

#include<iostream.h>

#include<conio.h>

class a       //Grand Parent class a

{

public:

void dis()

{

cout<<”dis”<<endl;

}};

class b             //Parent class b

{

public:

void show()

{

cout<<”show”<<endl;

}

};

class c:public b,public a                      //Child class c inherit the class a,class b

{

public:

void print()

{

cout<<”print”;

}

};

void main()

{

clrscr();

class c c1;

c1.dis();

c1.show();

c1.print();

getch();

}

/*output:

dis

show

print*/

3.) Multilevel Inheritance:

In Multiple inheritance one Grandparent class,one parent class and one child class.like below

 

 

 

 

 

 

/* Program of multilevel inheritance*/

#include<iostream.h>

#include<conio.h>

class a                        //Parent Class a

{

public:

void dis()

{

cout<<”dis”<<endl;

}}

class b::public a     //Child class b

{

public:

void show()

{

cout<<”show”<<endl;

}

class c:public b

{

public:

void print()

{

cout<<”print”;

}};

void main()

{

clrscr();

class c c1;

c1.dis();

c1.show();

c1.print();

getch();

}

/* output:

dis

show

print*/

4.) Hierarchical   Inheritance:

In hierarchical inheritance One Parent class and two child class inherit the properties of parent class .for examole

 

 

 

 

 

/* Program of hierarchical inheritance*/

#include<iostream.h>

#include<conio.h>

class a      //Parent class a

{

public:

void dis()

{

cout<<”dis”<<endl;

}

};

class b:public a      //child class b inherit the parent class a

{

public:

void show()

{

cout<<”show”<<endl;

}

};

class c:public b        //new child class inherit the parent class b

{

public:

void print()

{

cout<<”print”<<endl;

}

};

void main()

{

clrscr();

class b b1;

class c c1;

b1.dis();

b1.show();

c1.dis();

c1.print();

getch();

}

/* output:

dis

show

dis

print*/

5.) Hybrid Inheritance:

Combination of any two or more inheritance is known as Hybrid Inheritance. for example

 

 

 

 

 

 

 

 

/* Program of hybrid inheritance*/

#include<iostream.h>

#include<conio.h>

class a      //Grand Parent class a

{

public:

void dis()

{

cout<<”dis”<<endl;

}

};

class b:public a   //Parent class b

{

public:

void show()

{

cout<<”show”<<endl;

}

};

class c: public b  //Child class c

{

public:

void create()

{

cout<<”create”<<endl;

}

};

class d:public b   //Child class d

{

public:

void print()

{

cout<<”print”;

}

};

void main()

{

clrscr();

class a c1;

c1.dis();

c1.show();

class c c2;

c2.create();

class d c3;

c3.print();

getch();

}

/*output:

dis

show

create

print*/

7. Message passing:

An object-oriented program , consists of a set of objects that communicate with each other.

The process of programming in a  object-oriented language therefore involves the following basic steps:

  1. Creating classes that define objects and their behavior.
  2. Creating objects from class definitions.
  3. Establishing communication among objects.

Objects communicate with each other by sending and receiving information      in the same way as people pass messages to each other. The concept of message passing , make it easier to talk about building systems that directly model or simulate their real-world counterparts.

A message for an object is a request for execution of a procedure, and  therefore will invoke a function (procedure) that generates the desired result. Message passing involves specifying the name of the object, the name of the function (message) and the information to be sent.

Comments are closed.