Вы находитесь на странице: 1из 87

Edited by Foxit Reader

Copyright(C) by Foxit Software Company,2005-2007


For Evaluation Only.

C++ Programs

1. Program to show the use of "enum" data type


2. Program to show the use of Pointer to Constant and Constant Pointer
3. Program to show the use of Reference Variables.
4. Program to show the use of Extern Variables
5. Program to show the use of New and Delete operator
6. Program to show Function Overloading
7. Program to show Call By Value, Call by Address and Call By Reference
8. Program to show the use of Default Arguments
9. Program to show the use of const arguments
10. Program to show return by Reference
11. Program to show the use Pointer to Function
12. Program to show the use of Class and Object
13. Program to show the use of Static Members
14. Program to show the use of Friend Functions
15. Program to show the use of Friend Class
16. Program to show the use of Const Member Functions
17. Program to show the use of Inner Classes
18. Program to show the use of Default Constructor
19. Program to show the use of Parameterised Constructor with overloading
20. Program to show the use of Copy Constructor
21. Program to show the use of Destructors
22. Program to show how a Destructor can be called Explicitly.
23. Program to show the use of Operator Overloading(Operator +=)
24. Program to show the use of Operator Overloading (Operator + and =)
25. Program to show the use of Operator Overloading (Operator ++)
26. Program to show Operator Overloading using Friend functions
27. Program to show the use of Operator Overloading ( << operator)
28. Program to show the Overloading of int() typecast operation
29. Program to show the use of Public, Private and Protected Modes
30. Program to show Single Level Public Inheritance

Developed by Partha Roy Page 1


31. Program to show Single Level Private Inheritance
32. Program to show Single Level Protected Inheritance
33. Program to show the use of Multilevel (Public) Inheritance
34. Program to show the use of Multiple (Public) Inheritance
35. Program to show the use of Hierarchical (Public) Inheritance
36. Program to show Hybrid Public Inheritance
37. Program to show the calling of Constructors during Single Level
Inheritance
38. Program to show the calling of Constructors during Multilevel Inheritance
39. Program to show the calling of Constructors during Multiple Inheritance
40. Program to show the calling of Constructors during Hierarchical
Inheritance
41. Program to show the use of Pointer to Objects
42. Program to show the use of Virtual Functions
43. Program to show the use of Pure Virtual Functions and Abstract class
44. Program to show the use of width(),fill() & precision() functions
45. Program to show the use of open(),close(),put(),get() & eof() Disk File IO
functions
46. Program to show the use of seekp(),seekg(),tellp() & tellg() Disk File IO
functions
47. Program to show the use of read() & write() Disk IO Functions
48. Program to show the use of Template Functions
49. Program to show the use of Template Classes
50. Program to show the use of Throw, Try and Catch statements

Developed by Partha Roy Page 2


Experiment No. 1
#include<iostream.h>
#include<conio.h>
#include<dos.h>

// Program to Show the use of "enum" data type

main()
{
enum day{Sun, Mon, Tue, Wed, Thu, Fri, Sat};
int dy;
cout<<"\nEnter Day:- ";
cin>>dy;
switch(dy)
{
case Sun: cout<<"\nSUNDAY"; break;
case Mon: cout<<"\nMONDAY"; break;
case Tue: cout<<"\nTUESDAY"; break;
case Wed: cout<<"\nWEDNUSDAY"; break;
case Thu: cout<<"\nTHURSDAY"; break;
case Fri: cout<<"\nFRIDAY"; break;
case Sat: cout<<"\nSATURDAY"; break;
default: cout<<"Invalid Day";
}
getch();
}

Developed by Partha Roy Page 3


Experiment No. 2
#include<iostream.h>
#include<conio.h>
#include<dos.h>
// Program to Show the use of Pointer to Constant and Constant Pointer

main()
{
clrscr();
int a=10;
int b=20;
int c=30;
// Constant Pointer
int* const p=&a;
cout<<"\n*p= "<<*p;
*p=100;
cout<<"\na= "<<a;
// p=&b; This can not be done as pointer p is a const pointer.
//Pointer to Constant
const int *q=&b;
q=&a;
cout<<"\n*q= "<<*q;
q=&b;
// *q=200; This can not be done as pointer q is a pointer to constant
// so using q we can not change the value of b;
cout<<"\n*q= "<<*q;
//Constant Pointer to Constant
const int* const z=&c;
// z=&b; This can not be done as its const pointer
// *z=100; This can not be done as its pointer to constant
cout<<"\n*z= "<<*z;
getch();
}

Developed by Partha Roy Page 4


Experiment No. 3
#include<iostream.h>
#include<conio.h>

// Program to show the use of Reference Variables.

main()
{
clrscr();
int a=10;
int &r=a;
cout<<"\nr= "<<r;
cout<<"\na= "<<a;
a=100;
cout<<"\nr= "<<r;
cout<<"\na= "<<a;
r=200;
cout<<"\nr= "<<r;
cout<<"\na= "<<a;
getch();
}

Developed by Partha Roy Page 5


Experiment No. 4
#include<iostream.h>
#include<conio.h>

// Program to show the use of Extern Variables

void fun()
{
extern int a;
cout<<"\nFrom fun Function a= "<<a;
}

int a=100;

main()
{
clrscr();
extern int a;
cout<<"\nFrom main Function a= "<<a;
fun();
getch();
}

Developed by Partha Roy Page 6


Experiment No. 5
#include<iostream.h>
#include<conio.h>

// Program to show the use of New and Delete operator

main()
{
clrscr();
int *p,*q,*r;
int val,max;
p=new int;
cout<<"\nEnter a Value:- ";
cin>>*p;
cout<<"\n *p= "<<*p;
delete p;
cout<<"\nEnter a Value:- ";
cin>>val;
q=new int(val);
cout<<"\n *q= "<<*q;
delete q;
cout<<"\nEnter Max Items:- ";
cin>>max;
r=new int[max];
for(int i=0;i<max;i++)
{
cout<<"\nEnter value no "<<i+1<<" ";
cin>>r[i];
}
cout<<"\nEntered values are ";

Developed by Partha Roy Page 7


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
for(i=0;i<max;i++) For Evaluation Only.

{
cout<<" "<<r[i];
}

delete []p;
/* This is also possible
for(i=0;i<max;i++)
delete (int*) r[i];
*/
delete r;
getch();
}

Developed by Partha Roy Page 8


Experiment No. 6
#include<iostream.h>
#include<conio.h>

// Program to show Function Overloading

void add(int a, int b)


{
cout<<"\nFrom add function with 2 arguments a+b:- "<<a+b;
}
void add(int a, int b, int c)
{
cout<<"\nFrom add function with 3 arguments a+b+c:- "<<a+b+c;
}
main()
{
clrscr();
add(10,20);
add(10,20,30);
getch();
}

Developed by Partha Roy Page 9


Experiment No. 7
#include<iostream.h>
#include<conio.h>

// Program to show the use of Call by Value, Call by Address & Call by Reference

void CallByVal(float a)
{
a=100; // The local variable "a" 's value is changed locally
}
void CallByAdd(int *p)
{
*p=200; // The pointer "p" changes the value of the variable whose address
// it has
}
void CallByRef(int &r)
{
r=300; // The reference variable "r" changes the value of the
// original variable, whose reference it was created
}
main()
{
clrscr();
int a=10;
cout<<a;
CallByVal((float)a);
cout<<a;
CallByAdd(&a);
cout<<a;
CallByRef(a);
cout<<a;
getch();
}

Developed by Partha Roy Page 10


Experiment No. 8
#include<iostream.h>
#include<conio.h>

// Program to show the use of Default Arguments

void fun(int a, int b=1)


{
if(b==1)
{
cout<<"\nUsing default value 1 for second argument";
}
cout<<"\na+b= "<<a+b;
}
main()
{
clrscr();
fun(10,20);
fun(10);
getch();
}

Developed by Partha Roy Page 11


Experiment No. 9
#include<iostream.h>
#include<conio.h>

//Program to show the use of const arguments

void fun(const int i)


{
cout<<"\ni= "<<i;
// i=100; This can not be done as "i" is declared as constant
}
main()
{
clrscr();
fun(200);
getch();
}

Developed by Partha Roy Page 12


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

Experiment No. 10
#include<iostream.h>
#include<conio.h>

// Program to show return by Reference

int& Greater(int &r1,int &r2)


{
if(r1>r2)
{
return r1;
}
else
{
return r2;
}
}

main()
{
clrscr();
int a,b;
cout<<"\nEnter data for a= ";
cin>>a;
cout<<" Enter data for b= ";
cin>>b;
Greater(a,b)=100; // Here the greater value containing variable will (to show the property of reference some
value should be assigned to it)
//be returned and so its value
//would be replaced by 100

9to
Developed by Partha Roy Page 13
if(a==100)
{
cout<<"\na>b";
}
else
{
cout<<"\nb>a";
}
getch();
}

Developed by Partha Roy Page 14


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

Experiment No. 11
#include<iostream.h>
#include<conio.h>

// Program to show the use Pointer to Function

void Add(int a, int b)


{
cout<<a+b;
}
main()
{
clrscr();
void(*p)(int,int)=Add;// Assigning the address of a Function to a Pointer
// Here the *p should be enclosed in ()
(*p)(10,20); // While calling also () is required to first de-reference
// p and then the call is initiated with the input arguments
getch();
}

Developed by Partha Roy Page 15


Experiment No. 12
#include<iostream.h>
#include<conio.h>

// Program to show the use of Class and Object

class MyClass
{
int data;// Private data member.
//They can not be accessed from outside the class definition.
public:
void fill() // Public function member
{
cout<<"\nEnter value for data= ";
cin>>data; // "data" accessible inside the class
}
void show(); // The Member functions can be defined outside the class
};

void MyClass::show()
{
cout<<"\ndata= "<<data; //"data" is accessible because the declaration of show()
is
// inside the class
}

Developed by Partha Roy Page 16


main()
{
clrscr();
MyClass ob1; //"ob1" object created from class MyClass
// object "ob1" would contain a copy
//of the entire class definition
ob1.fill();
ob1.show();
// fill() and show() are public members so are accessible outside the class
getch();
}

Developed by Partha Roy Page 17


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

Experiment No. 13
#include<iostream.h>
#include<conio.h>

// Program to show the use of Static Members

class MyClass
{
static int cnt;
public:
static void count() // static member functions can only access
// static data members
{
cnt+=1;
}
void show()
{
cout<<"\nOutput from show() function :- count() called "<<cnt<<" times";
// non static functions can access static members
}
};
int MyClass::cnt; (should be declared just ouytside class)
// This creates the static member and initilizes it to 0
// This initilization is invoked only when the first object of
// the class is created

Developed by Partha Roy Page 18


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

main()
{
clrscr();
MyClass ob1;
ob1.show();
MyClass::count();// This is the only way to call/use static members
ob1.show();
MyClass::count();
ob1.show();
MyClass::count();
ob1.show();
getch();
}

Developed by Partha Roy Page 19


Experiment No. 14
#include<iostream.h>
#include<conio.h>

// Program to show the use of Friend Functions

class MyClass2;
class MyClass1
{
int a;
public:
void fill()
{
cout<<"\nEnter data for MyClass1:- ";
cin>>a;
}
friend void show(MyClass1 ob1, MyClass2 ob2);
};
class MyClass2
{
int b;
public:
void fill()
{
cout<<"\nEnter data for MyClass2:- ";
cin>>b;
}
friend void show(MyClass1 ob1, MyClass2 ob2);
};

Developed by Partha Roy Page 20


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.
void show(MyClass1 ob1, MyClass2 ob2)
{
cout<<"\nPrivate data of MyClass1:- "<<ob1.a;
cout<<"\nPrivate data of MyClass2:- "<<ob2.b;
}
main()
{
clrscr();
MyClass1 ob1;
MyClass2 ob2;
ob1.fill();
ob2.fill();
show(ob1,ob2);
getch();
}

Developed by Partha Roy Page 21


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
Experiment No. 15 For Evaluation Only.

#include<iostream.h>
#include<conio.h>

// Program to show the use of Friend Class

class MyClass1 //varriables in one class and functions in other class


{
int a;
friend class MyClass2;
};
class MyClass2
{
public:
void fill(MyClass1 &ob) //pass by reference

{
cout<<"\nEnter data for MyClass1:- ";
cin>>ob.a;
//since class2 is sa friend of class1
} so it can access its private memberd directly through object
and no need of public functions
void show(MyClass1 ob)
{
cout<<"\nData of MyClass1 from MyClass2:- "<<ob.a;
}
};
main()
{
clrscr();
MyClass1 ob1;
MyClass2 ob2;
ob2.fill(ob1);
ob2.show(ob1);
getch();
}

Developed by Partha Roy Page 22


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

Experiment No. 16
#include<iostream.h>
#include<conio.h>

// Program to show the use of Const Member Functions

class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter data for MyClass:- ";
cin>>a;
}
void show() const
{
// a=100; This cannot be done as show() is a const function
cout<<"\nData of MyClass:- "<<a;
}
};
main()
{
clrscr();
MyClass ob;
ob.fill();
ob.show();
getch();
}

Developed by Partha Roy Page 23


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

Experiment No. 17
#include<iostream.h>
#include<conio.h>

// Program to show the use of Inner Classes

class MyClass1
{
int a;
class MyClass2
{ mainly used to secure data of one class from another &
int b; when it is needed that any operation on ne data will automati-
cally do the same on other,
public:
void fill()
{
cout<<"\nEnter Data for MyClass2:- ";
cin>>b;
}
void show()
{
cout<<"\nData of MyClass2:- "<<b;
}
}ob1;
public:
void fill()
{
cout<<"\nEnter Data for MyClass1:- ";
cin>>a;
ob1.fill();
}

Developed by Partha Roy Page 24


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

void show()
{
cout<<"\nData of MyClass1:- "<<a;
ob1.show();
}
};
main()
{
clrscr();
MyClass1 ob;
MyClass1::MyClass2 ob2; we can make object of inner class of private part like this

ob.fill();
ob.show();
ob2.fill();
ob2.show();
getch();
}

Developed by Partha Roy Page 25


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

Experiment No. 18
#include<iostream.h>
#include<conio.h>

//Program to show the use of Deafult Constructors

class MyClass
{
int a;
public:
MyClass()
{
cout<<"\nDefault Constructor Invoked";
a=10;
}
void show()
{
cout<<"\nThe data in MyClass:- "<<a;
}
};
main()
{
clrscr();
MyClass ob1;
MyClass ob2=MyClass();
MyClass *op1;// This will Not invoke the Constructor function
// But "MyClass *op1=new MyClass();" this will invoke the constructor
ob1.show();
ob2.show();
getch();
}

Developed by Partha Roy Page 26


Experiment No. 19
#include<iostream.h>
#include<conio.h>

// Program to show the use of Parameterised Constructor with overloading

class MyClass
{
int a;
public:
MyClass()
{
cout<<"\nDefault Constructor Invoked";
a=10;
}
MyClass(int z)
{
cout<<"\nParameterised Constructor Invoked";
a=z;
}
void show()
{
cout<<"\nData in MyClass:- "<<a;
}
};

Developed by Partha Roy Page 27


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

main()
{
clrscr();
MyClass ob1,ob2(100); //This is called Implicit call to the constructor
MyClass ob3;// This way also objects can be created
//This called Explicit call to the constructor
ob3=MyClass(200);
ob1.show();
ob2.show();
ob3.show();
getch();
}

Developed by Partha Roy Page 28


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

Experiment No. 20
#include<iostream.h>
#include<conio.h>

// Program to show the use of Copy Constructor

class MyClass
{
int a;
// The accessibility of a constructor depends upon the
// access mode under which it had been declared
public:
MyClass(){}// This default constructor is needed for creating simple objects
MyClass(MyClass &ob)
{
cout<<"\nCopy Constructor Called";
a=ob.a;
}
void fill()
{
cout<<"\nEnter data for MyClass:- ";
cin>>a;
}
void show()
{
cout<<"\nThe data of MyClass:- "<<a;
}
};

Developed by Partha Roy Page 29


Edited by Foxit Reader
Copyright(C) by Foxit Software Company,2005-2007
For Evaluation Only.

main()
{
clrscr();
MyClass ob1,ob2;
ob1.fill();
ob2.fill();
ob1.show();
ob2.show();
MyClass ob3(ob1);//Here we can also use = to invoke the COPY
CONSTRUCTOR
//i.e during INITIALIZATION not during ASSIGNMETNT
ob3.show();
ob3.show();
getch();
}

Developed by Partha Roy Page 30


Experiment No. 21
#include<iostream.h>
#include<conio.h>
#include<string.h>

//Program to show the use of Destructors

class MyClass
{
char obname[20];
public:
MyClass(char *onm)
{
cout<<"\nDefault Constructor Invoked for object name:"<<onm;
strcpy(obname,onm);
}
~MyClass()
{
cout<<"\nDestructor Invoked for Object:- "<<obname;
}
};
main()
{
clrscr();
MyClass ob1("First"),ob2("Second"),ob3("Third");
getch();
}

Developed by Partha Roy Page 31


Experiment No. 22
#include<iostream.h>
#include<conio.h>
#include<string.h>
// Program to show how a Destructor can be called Explicitly.
class A
{
char obname[20];
public:
A(char *nm)
{
cout<<"\nContructor Called";
strcpy(obname,nm);
cout<<"\nObject Name:- "<<obname;
}
~A()
{
cout<<"\nDestructor Called "<<this->obname;
}
};
main()
{
clrscr();
A ob1("Object1");
cout<<"\n";
A ob2=A("Object2");
cout<<"\n";
ob1.A::~A(10);
cout<<"\n";
ob2.A::~A(20);
cout<<"\n";
getch();
}

Developed by Partha Roy Page 32


Experiment No. 23
#include<iostream.h>
#include<conio.h>

// Program to show the use of Operator Overloading (Unary Operator +=)

class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter the data for MyClass ";
cin>>a;
}
void show()
{
cout<<"\nThe data of MyClass "<<a;
}
//The calling object is the operand in the L.H.S of the operator +=
//and the input argument is the R.H.S operand of the operator +=
void operator +=(MyClass ob)
{
a=a+ob.a;
}
};

Developed by Partha Roy Page 33


main()
{
clrscr();
MyClass ob1,ob2;
ob1.fill();
ob2.fill();
ob1.show();
ob2.show();
ob1+=ob2;// " ob1.operator +=(ob2); " can also be used
cout<<"\nAfter +=:- ";
ob1.show();
getch();
}

Developed by Partha Roy Page 34


Experiment No. 24
#include<iostream.h>
#include<conio.h>

// Program to show the use of Operator Overloading (Binary Operator + and =)

class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter the data for MyClass ";
cin>>a;
}
void show()
{
cout<<"\nThe data of MyClass "<<a;
}
// Here the object in the LHS is the operand calling this function
// The object in the RHS is the operand as the input argument of this function
// The returned object is the resulting object due to the operation "+"

Developed by Partha Roy Page 35


MyClass operator +(MyClass ob)
{
cout<<"\nOverloaded '+' operator called ";
MyClass tmp;
tmp.a=a+ob.a;
}
void operator =(MyClass ob)
{
cout<<"\nOverloaded '=' operator called ";
a=ob.a;
}
};
main()
{
clrscr();
MyClass ob1,ob2,ob3;
ob1.fill();
ob2.fill();
ob1.show();
ob2.show();
ob3=ob1+ob2;//Equivalent calling " ob3.operator =(ob1.operator +(ob2)); "
cout<<"\nAfter Add:- ";
ob3.show();
getch();
}

Developed by Partha Roy Page 36


Experiment No. 25
#include<iostream.h>
#include<conio.h>

// Program to show the use of Operator Overloading (Operator ++)

class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter the data for MyClass ";
cin>>a;
}
void show()
{
cout<<"\nThe data of MyClass "<<a;
}
void operator ++() //Here the calling object itself is the Operand
{
cout<<"\nOverloaded '++' operator called ";
a+=1;
}

};

Developed by Partha Roy Page 37


main()
{
clrscr();
MyClass ob1;
ob1.fill();
ob1.show();
ob1++; //Here we can use " ob1.operator++(); " to do the same thing
cout<<"\nAfter ++:- ";
ob1.show();
getch();
}

Developed by Partha Roy Page 38


Experiment No. 26
#include<iostream.h>
#include<conio.h>

//Program to show Operator Overloading using Friend functions

class A
{
int a;
public:
A()
{
a=10;
}
//LHS + //RHS
friend void operator +(A &ob1, A&ob2)
{
cout<<"\n"<<ob1.a+ob2.a;
}
//operand
friend void operator ++(A &ob)
{
ob.a+=1;
cout<<"\n"<<ob.a;
}
};
main()
{
clrscr();
A ob1,ob2;

//LHS + //RHS
ob1 + ob2;

//operand
ob1++;

getch();
}

Developed by Partha Roy Page 39


Experiment No. 27
#include<iostream.h>
#include<conio.h>
// Program to show the use of Operator Overloading ( << operator)

class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter data for MyClass ";
cin>>a;
}
// Here we need to use the Friend keyword for two input parameters
// This is because the original cout<< syntax will only allow single
// input parameter.
// For allowing parameters of two different classes i.e ostream and MyClass
// we need to make it Friend.
friend ostream& operator << (ostream &out,MyClass ob)
{
out<<"\nData in MyClass "<<ob.a;
return out;
}
};
main()
{
clrscr();
MyClass ob1;
ob1.fill();
cout<<ob1;
getch();
}

Developed by Partha Roy Page 40


Experiment No. 28
#include<iostream.h>
#include<conio.h>

// Program to show the Overloading of int() typecast operation

class MyClass
{
int a;
public:
operator int()// Overloading "int()" operation
{
return a;
}
MyClass(int z)
{
a=z;
}
};
main()
{
clrscr();
MyClass ob1(100);
int in=int(ob1); //Overloaded "int()" Operator called
cout<<in;
getch();
}

Developed by Partha Roy Page 41


Experiment No. 29
#include<iostream.h>
#include<conio.h>

// Program to show the use of Public,Private and Protected Modes

class MyClass
{
private:
int a;
void fill()
{
cout<<"\nEnter data for MyClass:- ";
cin>>a;
}

protected:
void show()
{
cout<<"\nData of MyClass:- "<<a;
}

public:
void callfill()
{
fill(); // Calling "fill()" which is defined under Private mode
}
void callshow()
{
show(); // Calling "show()" which is defined under Protected mode
}
};

Developed by Partha Roy Page 42


main()
{
clrscr();
MyClass ob1;
// ob1.fill(); "fill()" is not accessible from outside the class
// as it is under private mode
// ob1.show(); "show()" is not accessible from outside the class
// as it is under protected mode
ob1.callfill(); //"callfill()" is accessible as its under public mode
ob1.callshow(); //"callshow()" is accessible as its under public mode
getch();
}

Developed by Partha Roy Page 43


Experiment No. 30
#include<iostream.h>
#include<conio.h>

// Program to show Single Level Public Inheritance

class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};

class Child:public Parent


{
int b;
public:
void fill()
{
cout<<"\nEnter data for Child Class:- ";
cin>>b;
}

Developed by Partha Roy Page 44


void show()
{
cout<<"\nData of Child Class:- "<<b;
}
};

main()
{
clrscr();
Child c1;
//Here the Child class functions are used
c1.fill();
c1.show();
cout<<"\n";
//Here Parent class functions are used through Child class Onjects
c1.Parent::fill();
c1.Parent::show();
getch();
}

Developed by Partha Roy Page 45


Experiment No. 31
#include<iostream.h>
#include<conio.h>

// Program to show Single Level Private Inheritance

class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};

class Child:private Parent


{
int b;
public:
void ParentFill()
{
Parent::fill(); //It is under Private mode
}

Developed by Partha Roy Page 46


void ParentShow()
{
Parent::show(); //It is under Private mode
}
void fill()
{
cout<<"\nEnter data for Child Class:- ";
cin>>b;
}
void show()
{
cout<<"\nData of Child Class:- "<<b;
}
};

main()
{
clrscr();
Child c1;
//Here the Child class functions are used
c1.fill();
c1.show();
cout<<"\n";
c1.ParentFill();
c1.ParentShow();
/* Here Parent class functions can not be called as they all are private
in Child class.
c1.Parent::fill();
c1.Parent::show();
*/
getch();
}

Developed by Partha Roy Page 47


Experiment No. 32
#include<iostream.h>
#include<conio.h>

// Program to show Single Level Protected Inheritance

class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};

class Child:protected Parent


{
int b;
public:
void ParentFill()
{
Parent::fill(); // Its under Protected mode
}

Developed by Partha Roy Page 48


void ParentShow()
{
Parent::show(); // Its under Protected mode
}
void fill()
{
cout<<"\nEnter data for Child Class:- ";
cin>>b;
}
void show()
{
cout<<"\nData of Child Class:- "<<b;
}
};

main()
{
clrscr();
Child c1;
//Here the Child class functions are used
c1.fill();
c1.show();
cout<<"\n";
c1.ParentFill();
c1.ParentShow();
/* Here Parent class functions can not be called as they all are
under Protected mode in the Child class.
c1.Parent::fill();
c1.Parent::show();
*/
getch();
}

Developed by Partha Roy Page 49


Experiment No. 33
#include<iostream.h>
#include<conio.h>

// Program to show the use of Multilevel Public Inheritance

class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};

class Child1 : public Parent


{
int b;
public:
void fill()
{
cout<<"\nEnter data for Child1 Class:- ";
cin>>b;
}

Developed by Partha Roy Page 50


void show()
{
cout<<"\nData of Child1 Class:- "<<b;
}
};

class Child2 : public Child1


{
int c;
public:
void ParentFill()
{
Parent::fill();
}
void ParentShow()
{
Parent::show();
}
void Child1Fill()
{
Child1::fill();
}
void Child1Show()
{
Child1::show();
}
void fill()
{
cout<<"\nEnter data for Child2 Class:- ";
cin>>c;
}

Developed by Partha Roy Page 51


void show()
{
cout<<"\nData of Child2 Class:- "<<c;
}
};
main()
{
clrscr();
Child2 c2;
c2.fill();
c2.show();
cout<<"\n";
c2.Child1Fill();
c2.Child1Show();
cout<<"\n";
c2.ParentFill();
c2.ParentShow();
getch();
}

Developed by Partha Roy Page 52


Experiment No. 34
#include<iostream.h>
#include<conio.h>

// Program to show the use of Multiple Public Inheritance

class Parent1
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent1 Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent1 Class:- "<<a;
}
};

class Parent2
{
int b;
public:
void fill()
{
cout<<"\nEnter data for Parent2 Class:- ";
cin>>b;
}

Developed by Partha Roy Page 53


void show()
{
cout<<"\nData of Parent2 Class:- "<<b;
}
};

class Child:public Parent1, public Parent2


{
int c;
public:
void ParentFill()
{
Parent1::fill();
Parent2::fill();
}
void ParentShow()
{
Parent1::show();
Parent2::show();
}
void fill()
{
cout<<"\nEnter data for Child Class:- ";
cin>>c;
}
void show()
{
cout<<"\nData of Child Class:- "<<c;
}
};

Developed by Partha Roy Page 54


main()
{
clrscr();
Child c;
c.fill();
c.show();
cout<<"\n";
c.ParentFill();
c.ParentShow();
getch();
}

Developed by Partha Roy Page 55


Experiment No. 35
#include<iostream.h>
#include<conio.h>

//Program to show Hierachical Public Inheritance

class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};

class Child1:public Parent


{
int b;
public:
void fill()
{
cout<<"\nEnter data for Child1 Class:- ";
cin>>b;
}

Developed by Partha Roy Page 56


void show()
{
cout<<"\nData of Child1 Class:- "<<b;
}
void fillParent()
{
Parent::fill();
}
void showParent()
{
Parent::show();
}
};

class Child2:public Parent


{
int c;
public:
void fill()
{
cout<<"\nEnter data for Child2 Class:- ";
cin>>c;
}
void show()
{
cout<<"\nData of Child2 Class:- "<<c;
}
void fillParent()
{
Parent::fill();
}

Developed by Partha Roy Page 57


void showParent()
{
Parent::show();
}
};

main()
{
clrscr();
Child1 c1;
Child2 c2;
c1.fill();
c1.show();
cout<<"\n";
c2.fill();
c2.show();
cout<<"\n";
c1.fillParent();
c1.showParent();
cout<<"\n";
c2.fillParent();
c2.showParent();
getch();
}

Developed by Partha Roy Page 58


Experiment No. 36
#include<iostream.h>
#include<conio.h>

//Program to show Hybrid Public Inheritance

class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class: ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};

class ChildR:virtual public Parent


{
int b;
public:
void fill()
{
cout<<"\nEnter data for ChildR Class: ";
cin>>b;
}

Developed by Partha Roy Page 59


void show()
{
cout<<"\nData of ChildR Class:- "<<b;
}
void fillParent()
{
Parent::fill();
}
void showParent()
{
Parent::show();
}
};

class ChildL:virtual public Parent


{
int c;
public:
void fill()
{
cout<<"\nEnter data for ChildL Class: ";
cin>>c;
}
void show()
{
cout<<"\nData of ChildL Class:- "<<c;
}
void fillParent()
{
Parent::fill();
}

Developed by Partha Roy Page 60


void showParent()
{
Parent::show();
}
};

class ChildM:public ChildR, public ChildL


{
int d;
public:
void fill()
{
cout<<"\nEnter data for ChildM Class: ";
cin>>d;
}
void show()
{
cout<<"\nData of ChildM Class:- "<<d;
}
void fillParent()
{
ChildR::fill();
ChildL::fill();
Parent::fill();
}
void showParent()
{
ChildR::show();
ChildL::show();
Parent::show();
}
};

Developed by Partha Roy Page 61


main()
{
clrscr();
ChildM cm;
cm.fill();
cm.show();
cout<<"\n";
cm.fillParent();
cm.showParent();
getch();
}

Developed by Partha Roy Page 62


Experiment No. 37
#include<iostream.h>
#include<conio.h>

//Program to show the calling of Constructors during Single Level Inheritance

class Parent
{
int a;
public:
Parent(int z)
{
a=z;
}
void show()
{
cout<<"\nData of Parent Class "<<a;
}
};

class Child:public Parent


{
int b;
public:
Child(int x, int y):Parent(y)
{
b=x;
}

Developed by Partha Roy Page 63


void show()
{
cout<<"\nData of Child Class "<<b;
Parent::show();
}
};
main()
{
clrscr();
Child c(10,20);
c.show();
getch();
}

Developed by Partha Roy Page 64


Experiment No. 38
#include<iostream.h>
#include<conio.h>

//Program to show the calling of Constructors during Multilevel Inheritance

class Parent
{
int a;
public:
Parent(int z)
{
a=z;
}
void show()
{
cout<<"\nData of Parent Class "<<a;
}
};

class Child1:public Parent


{
int b;
public:
Child1(int x, int y):Parent(y)
{
b=x;
}

Developed by Partha Roy Page 65


void show()
{
cout<<"\nData of Child1 Class "<<b;
Parent::show();
}
};

class Child2:public Child1


{
int c;
public:
Child2(int x, int y, int z):Child1(y,z)
{
c=x;
}
void show()
{
cout<<"\nData of Child2 Class "<<c;
Child1::show();
}
};

main()
{
clrscr();
Child2 c(10,20,30);
c.show();
getch();
}

Developed by Partha Roy Page 66


Experiment No. 39
#include<iostream.h>
#include<conio.h>

//Program to show the calling of Constructors during Multiple Inheritance

class Parent1
{
int a;
public:
Parent1(int z)
{
a=z;
}
void show()
{
cout<<"\nData of Parent1 Class "<<a;
}
};

class Parent2
{
int b;
public:
Parent2(int x)
{
b=x;
}
void show()
{
cout<<"\nData of Parent2 Class "<<b;
}
};

Developed by Partha Roy Page 67


class Child:public Parent1,public Parent2
{
int c;
public:
Child(int x, int y, int z):Parent1(y),Parent2(z)
{
c=x;
}
void show()
{
cout<<"\nData of Child Class "<<c;
Parent1::show();
Parent2::show();
}
};

main()
{
clrscr();
Child c(10,20,30);
c.show();
getch();
}

Developed by Partha Roy Page 68


Experiment No. 40
#include<iostream.h>
#include<conio.h>

//Program to show the calling of Constructors during Hierarchical Inheritance

class Parent
{
int a;
public:
Parent(int z)
{
a=z;
}
void show()
{
cout<<"\nData of Parent Class "<<a;
}
};

class Child1:public Parent


{
int b;
public:
Child1(int x, int y):Parent(y)
{
b=x;
}

Developed by Partha Roy Page 69


void show()
{
cout<<"\nData of Child1 Class "<<b;
Parent::show();
}
};

class Child2:public Parent


{
int c;
public:
Child2(int x, int y):Parent(y)
{
c=x;
}
void show()
{
cout<<"\nData of Child2 Class "<<c;
Parent::show();
}
};

main()
{
clrscr();
Child1 c1(10,20);
c1.show();
Child2 c2(30,40);
c2.show();
getch();
}

Developed by Partha Roy Page 70


Experiment No. 41
#include<iostream.h>
#include<conio.h>

//Program to show the use of Pointer to Objects

class A
{
int a;
public:
A()
{
cout<<"Class A constructor Called ";
}
void fill()
{
cout<<"\nEnter data for Class A :- ";
cin>>a;
}
void show()
{
cout<<"\nData of Class A:- "<<a;
}
};

class B
{
int b;
public:

Developed by Partha Roy Page 71


void fill()
{
cout<<"\nEnter data for Class B :- ";
cin>>b;
}
void show()
{
cout<<"\nData of Class B:- "<<b;
}
};
class C:public A
{
int c;
public:
void fill()
{
cout<<"\nEnter data for Class C :- ";
cin>>c;
}
void show()
{
cout<<"\nData of Class A:- "<<c;
}
void fun()
{
fill();
show();
}
};

Developed by Partha Roy Page 72


main()
{
clrscr();
A *p;
B ob;
C ob2;

// p=&ob; //This cannot be done as class A is no way related to class B;


p=&ob2;
// Observe here that fill() and show() functions are declared in Class A
// as well as Class B
p->fill();// Although "p" has the address "ob2" but it would
p->show();// invoke the fill() and show() functions of its own class
//i.e class A.
// p->fun(); //This would cause an error as "p" can invoke only those
// functions which are in Class A because "p" is of type A

getch();
}

Developed by Partha Roy Page 73


Experiment No. 42
#include<iostream.h>
#include<conio.h>

//Program to show the use of Virtual Functions

class Parent
{
int a;
public:
virtual void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
virtual void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};

class Child:public Parent


{
int b;
public:
void fill()
{
cout<<"\nEnter data for Child Class:- ";
cin>>b;
}

Developed by Partha Roy Page 74


void show()
{
cout<<"\nData of Child Class:- "<<b;
}
};
main()
{
clrscr();
Parent *p;
Child c;
p=&c;
p->fill();
p->show();
getch();
}

Developed by Partha Roy Page 75


Experiment No. 43
#include<iostream.h>
#include<conio.h>

//Program to show the use of Pure Virtual Functions and Abstract class

class Parent
{
public:
// Functions fill() & show() are Pure Virtual Functions
// This causes Class Parent to become Abstract.
// Any class inheriting this class would automatically become Abstract
// To avoid this the child class should define the functions.
virtual void fill()=0;
virtual void show()=0;
};

class Child1:public Parent


{
int b;
public:
// fill() and show() functions are not defined
// So the Child class has automatically become ABSTRACT
// Objects of Abstract classes cannot be created.
void c1fill()
{
cout<<"\nEnter data for Child1 Class:- ";
cin>>b;
}

Developed by Partha Roy Page 76


void c1show()
{
cout<<"\nData of Child1 Class:- "<<b;
}
};
class Child2:public Child1
{
public:
// Now Child1 class is Abstract as Child2 has inherited Child1
// so Child2 will also become Abstract.
// To avoid this we would define the fill() and show() functions.
// Here the prototypes should strictly match.
void fill()
{
cout<<"\nfill() function defined in Child2 ";
c1fill();
}
void show()
{
cout<<"\nshow() function defined in Child2 ";
c1show();
}
};

Developed by Partha Roy Page 77


main()
{
clrscr();
// Child1 c1; This would create an error as Objects of Abstract Classes
// cannot be created.
Child2 c2;
c2.fill();
c2.show();
getch();
}

Developed by Partha Roy Page 78


Experiment No. 44
#include<iostream.h>
#include<conio.h>

//Program to show the use of width(),fill() & precision() functions

main()
{
clrscr();
float a,b,c;
cout<<"\nEnter a Float value :- ";
cin>>a;
cout<<"\nEnter a Float value :- ";
cin>>b;
cout<<"\nEnter a Float value :- ";
cin>>c;
cout<<"\nThe Entered Values Are:-\n";
cout.width(20);
cout.fill('!');
cout.precision(3);
cout<<a;
cout.width(20);
cout.fill('$');
cout.precision(2);
cout<<b;
cout.width(20);
cout.fill('#');
cout.precision(1);
cout<<c;
getch();
}

Developed by Partha Roy Page 79


Experiment No. 45
#include<iostream.h>
#include<fstream.h>
#include<conio.h>

//Program to show the use of open(),close(),put(),get() & eof() Disk File IO


functions

main()
{
clrscr();
fstream f;
char c1;
f.open("c:\\Test.txt",ios::out);//Opening file "c:\Test.txt" in write only mode
for(int i=1;i<=5;i++)
{
cout<<"\nEnter a Character:- ";
cin>>c1;
f.put(c1);//putting one character at a time into the file
}
f.close();
f.open("c:\\Test.txt",ios::in);//Opening file "c:\Test.txt" in read only mode
char c2;
while(!f.eof()) //eof() returns zero value when eof is not encountered
//otherwise it returns non zero value when eof is encountered.
{
f.get(c2);//getting one character at a time from the file
cout<<c2;
}
f.close();
getch();
}

Developed by Partha Roy Page 80


Experiment No. 46
#include<iostream.h>
#include<fstream.h>
#include<conio.h>

//Program to show the use of seekp(),seekg(),tellp() & tellg() Disk File IO


functions

main()
{
clrscr();
fstream f;
char c1;
f.open("c:\\Test2.txt",ios::out);//Opening file "c:\Test2.txt" in write mode
for(int i=1;i<=6;i++)
{
cout<<"\nEnter a Character:- ";
cin>>c1;
f.put(c1);//putting one character at a time into the file
}
cout<<"\n";
char c2;
cout<<"\nUsing tellg() function:- "<<f.tellg();
cout<<"\nUsing tellp() function:- "<<f.tellp();
//f.seekp(0,ios::beg);
f.seekg(0,ios::beg);

Developed by Partha Roy Page 81


cout<<"\nUsing f.seekp(0,ios::beg) and then tellp() function:- "<<f.tellp();
cout<<"\nUsing f.seekg(0,ios::beg) and then tellg() function:- "<<f.tellg();
// f.seekp(1,ios::beg);
f.seekg(1,ios::beg);
cout<<"\nUsing seekp(1,ios::beg) and then tellp() function:- "<<f.tellp();
cout<<"\nUsing seekg(1,ios::beg) and then tellg() function:- "<<f.tellg();
f.get(c1);
cout<<"\nUsing get(char) function:- "<<c1;
f.seekp(1,ios::cur);
f.seekg(1,ios::cur);
cout<<"\nUsing seekp(1,ios::cur) and then tellp() function:- "<<f.tellp();
cout<<"\nUsing seekg(2,ios::cur) and then tellg() function:- "<<f.tellg();
f.get(c1);
cout<<"\nUsing get(char) function:- "<<c1;
f.close();
getch();
}

Developed by Partha Roy Page 82


Experiment No. 47
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>

//Program to show the use of read() & write() Disk IO Functions

class Student
{
char name[40];
int roll;
public:
void fill()
{
cout<<"\nEnter The Name of the Student:- ";
gets(name);
cout<<"\nEnter Roll Number:- ";
cin>>roll;
}
void show()
{
cout<<"\nThe Name of the Student:- ";
puts(name);
cout<<"\nRoll Number:- "<<roll;
}
};

Developed by Partha Roy Page 83


main()
{
clrscr();
fstream f;
Student CSE;
f.open("c:\\Test3.txt",ios::app);//Opening the file in Append Mode
for(int i=1;i<=3;i++)
{
CSE.fill();
f.write((char*)&CSE, sizeof(CSE));
}
cout<<"\nWriting Data into File....\n";
f.close();

Student Temp;
f.open("c:\\Test3.txt",ios::in);//Opening the file in Read Mode
cout<<"\nReading Data From File....\n";
while(!f.eof())
{
f.read((char*)&Temp, sizeof(Temp));
Temp.show();
}
f.close();
getch();
}

Developed by Partha Roy Page 84


Experiment No. 48
#include<iostream.h>
#include<conio.h>

//Program to show the use of Template Functions

template<class T1,class T2>


void tfun(T1 a, T2 b)
{
cout<<"\nEnter data for a:- ";
cin>>a;
cout<<"\nEnter data for b:- ";
cin>>b;
cout<<"\nData of a:- "<<a;
cout<<"\nData of b:- "<<b;
};
main()
{
clrscr();
int x;
float y;
tfun(x,y);
getch();
}

Developed by Partha Roy Page 85


Experiment No. 49
#include<iostream.h>
#include<conio.h>

//Program to show the use of Template Classes

template<class T1,class T2>


class MyClass
{
T1 a;
T2 b;
public:
void fill()
{
cout<<"\nEnter data for a:- ";
cin>>a;
cout<<"\nEnter data for b:- ";
cin>>b;
}
void show()
{
cout<<"\nData of a:- "<<a;
cout<<"\nData of b:- "<<b;
}
};
main()
{
clrscr();
MyClass<int,float> ob1;
ob1.fill();
ob1.show();
getch();
}

Developed by Partha Roy Page 86


Experiment No. 50

#include<iostream.h>

//Program to show the use if Throw, Try and Catch statements

main()
{
try
{
throw(10);
/*Here the exception argument is an integer value 10, So it can be
caught by that case which has int in the parameter list, here implicit
type conversion is possible */
}
catch(int a)
{
cout<<"\nException Caught:- "<<a;
}
cout<<"\n";
}

Best of Luck

Developed by Partha Roy Page 87

Вам также может понравиться