You are on page 1of 17

C++ Practical File

Concept of Object Oriented Programming :In structured programming, the primary focus is on the function. In structured
programming program has a fixed well defined structure.
The major motivating factor in the invention of object-oriented approach is to
remove some of the flaws encountered in the procedural approach. OOP treats
data as a critical element in the program development and does not allow it flow
freely around the system. It ties data more closely to the functions that operate it,
and protects it from accidental modification from outside functions. OOP allows
decomposition of a problem into a number of entities called objects and then
builds data functions around these objects.
/*Program for smallest no. using object-oriented programming*/
#include<iostream.h>
#include<conio.h>
class small
{
private: int x,y;
public:
void readdata();
void output();
};
void small:: readdata()
{
cout<<"Enter first number";
cin>>x;
cout<<"Enter second number";
cin>>y;
}
void small:: output()
{
if(x < y)
cout<<"smallest is:" < < x< < endl;
else
cout<<"smallest is:" < < y< < endl;
}
void main()
{
small s;
clrscr();
s.readdata();
s.output();
getch();
}

output
Enter first number
5
Enter second number
7
smallest is:5

Concept of Access Modifier:There are three type of access modifier namely


1. Private
2. Public
3. Protected
Private :- The member that have been declared as private can be accessed only
from within class.
Public :- The member that have been declared as public can be accessed from
outside the class. Generally the functions are written under public specifier
because generally functions are called form outside the class in real life program.
Protected :- The protected member can be accessed within the class in which they
are defined and they are also accessed in derived class which is derived by its own
class. Note that protected member can not be accessed from outside these classes.
Concept of Inheritance:Inheritance is the most powerful feature of object-oriented programming classes
and objects. Inheritance is the process of creating a new class, called derived class
from existing class, called base class. The derived class inherits some or all the
traits from base class. The base class is unchanged by this process. Most important
advantage of inheritance is reusability. Once a base class is written and debugged,
it need not be touched again and we can used this class for deriving another class
if we need.
Type of Inheritance:1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Inheritance:- In single inheritance one class is derived from one base class.
Multiple Inheritance:- In this type of inheritance one class is derived from several
base classes.
Multilevel Inheritance:- In multilevel inheritance deriving a class from another
derived class.
Hierarchical Inheritance:- In hierarchical inheritance from one base class more
than one classes are derived.
Hybrid Inheritance:- If we more than one type of inheritance to design a problem
that is known is hybrid inheritance.
/*Program for handling arithmetic operation through inheritance*/
#include<iostream.h>
#include<conio.h>
class calculate
{
protected: int x,y;
public:
void assign()
{

x=10;
y=20;
}
};
class cal1: public calculate
{
int s;
public: void add()
{
s=x+y;
cout<<"x+y =";" < <s;" < <endl;
}
};
class cal2:public calculate
{
int t;
public: void sub()
{
t=x-y;
cout<<"x-y is=";" < <t;" < <endl;
}
};
class cal3:public calculate
{
int m;
public:void mul()
{
m=x*y;
cout<<"x*y is=";" < <m;" < <endl;
}
};
void main()
{
cal1 c1;
cal2 c2;
cal3 c3;
clrscr();
c1.assign();
c1.add();
c2.assign();
c2.sub();
c3.assign();
c3.mul();
getch();
}

Output:
x+y =30
x-y is=-10
x*y is=200

Constructor:- A constructor is special type of function for automatic initialization


of object. Whenever an object is created the constructor will be executed
automatically
The name of constructor must be same as that of its class. The constructor is
declared with no return type, not even void. Constructor may not be static and
virtual. Constructor should be declared in the public section, it can be declared
within the protected and in some rare case within the private.
Note that object with a constructor cannot be used as a member of a union.
/*Program for print the object whenever we create an object*/
#include<iostream.h>
#include<conio.h>
class counter
{
static int count;
public: counter()
{
count++;
cout<<"object number is " < <count < <endl;
}
};
int counter::count;
void main()
{
clrscr();
cout < <"we are creating c1:" < <endl;
counter c1;
cout < <"We are creating c2;" < <endl;
counter c2;
getch();
}

output
we are creating c1:
object number is 1
We are creating c2;
object number is 2

Destructor:-A destructor is used to destroy the objects that have been created by a
constructor. A destructor is a member function like constructor.
The name of destructor is same as constructor but is preceded by tilde(~).
Destructor will automatically be called by compiler upon exit from the program to
clean up storage which was taken by object. Note that the objects are destroyed
in the reverse order of creation.
/*Program for print the object whenever we create an object */
#include<iostream.h>
#include<conio.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout< <"Number of object created: "< <count< <endl;
}
~ alpha()
{
cout< <"number of objected destroyed "< <count<<endl;
count--;
}
};
void main()
{
clrscr();
cout< <"Enter main\n";
alpha a1,a2,a3,a4;
{
cout< <"enter block1";
alpha a5;
}
{
cout< <"enter block2";
alpha a6;
}
cout< <"\nRe-enter main";
getch();
}

OUTPUT
Enter main
Number of object created: 1
Number of object created: 2
Number of object created: 3
Number of object created: 4
enter block1Number of object created: 5
number of objected destroyed 5
enter block2Number of object created: 5
number of objected destroyed 5
Re-enter main

Function Overloading:Overloading refers to the use of the same thing for different purpose. This means
that we can use the same function name to create functions that perform a variety
of different tasks. This is known as function polymorphism in OOP.
/*Program for function overloading*/
#include<iostream.h>
#include<conio.h>
int volume(int);
double volume(double,int);
long volume(long,int,int);
void main()
{
cout<<volume(10)<<"\n";
cout<<volume(5.5,10)<<"\n";
cout<<volume(100l,75,15)<<"\n";
}
int volume(int a) //cube
{
return(a*a*a);
}
double volume(double b,int c) //cylinder
{
return(3.14*b*b*c);
}
long volume(long d,int e,int f) //rectanglular
{
return(d*e*f);
}

Result:
cube: 1000
cylinder: 949.85
rectangular: 112500

Virtual function:Virtual means existing in effect but not in reality. A function is declared virtual buy
writing keyword virtual in front of function header. The question is why virtual
function are needed? For the answer of question see the following example.
/*Programe for handling virtual function*/
#include<iostream.h>
#include<conio.h>
class B
{
public:
virtual void show()
{
cout<<"this is in class B"<<endl;
}
};
class D1:public B
{
public :void show()
{
cout<<"this is in class D1"<<endl;
}
};
class D2:public B
{
public :void show()
{
cout<<"this is in class D2"<<endl;
}
};
void main()
{
clrscr();
B *p;
D1 obj1;
D2 obj2;
B objbase;
p=&objbase;
p->show();
p=&obj1;
p->show();
p=&obj2;
p->show();
getch();
}

/*OUTPUT */
this is in class B
this is in class D1
this is in class D2

Class Template:Template is very important feature which has been added recently to c++. By using
templates we can define generic classes. We can define template for both classes
and function.
For example suppose we want to create a class which has three private data all are
int type. And the class has two member function one get the data from keyboard
and second perform the addition of two private data, store the result into third
private data, then print the result.
But suppose we want to perform same above operation with float type data then
what we do? The solution is we have to change the type of private data from into
to float, or design a new class which private data are float type. But with the help
of templates we can solve problem. Because template facility allows the data type
to be specified as a parameter. We can design a generic classes.
/*Programe which generate template class, by which we can perform integer type
data addition and float type data addition also*/
#include<iostream.h>
#include<conio.h>
template<class T>
class add
{
private: T a,b;
public: void getdata()
{
cout<<"enter first data =";
cin>>a;
cout<<"enter second data =";
cin>>b;
}
Tsum()
{
T c;
c=a+b;
return(c);
}
};
void main()
{
add<int>obj1;
add<float>obj2;
cout<<"enter integer number"<<endl;
obj1.getdata();
cout<<"Sum of integer data ="<<obj1.sum();
cout<<endl;
cout<<"Enter float type data"<<endl;
obj2.getdata();
cout<<"Sum of float type data"<<obj2.sum();

cout<<endl;
getch();
}

Operator Overloading:Operator overloading is one of the feature of C++ language. The concept by which
we can give special meaning to an operator of language is known as operator
overloading. For example + operator in C++ work only with basic type int and float
means. C=a+b is calculated by compiler if a, b and c are basic type, suppose a, b
and c are object of user defined class compiler give error. However using operator
overloading we can make this statement legal even if a, b and c are objects of
class.
Actually when we write statement C=a+b the compiler call a member function of
class. If a, b and c are basic type then compiler calculates a+b and assigns that of
c.
We can overload all the C++ operators except the following:
1. ?:(condition operator)
2. : : (scope resolution operator)
3. size of()size of operator)
4. .(Membership operator)
5. *(Pointer to member operator)
/*Programe for reading a complex no. increment real and imaginary part */
#include<iostream.h>
#include<conio.h>
class complex
{
int i,r;
public:
void getdata()
{
cout<<"enter real part:";
cin>>r;
cout<<"enter imag. part:";
cin>>i;
}
void operator ++() //overloading ++
{
++r;
++i;
}
void showdata()
{
cout<<r<<"+i"<<i<<endl;
}
};
void main()
{
complex c1;
c1.getdata();
cout<<"no. before increment"<<endl;
c1.showdata();

c1++;
cout<<"no. after increment"<<endl;
c1.showdata();
getch();
}
/*OUTPUT*/
enter real part:3
enter imag. part:4
no. before increment
3+i4
no. after increment
4+i5

You might also like