University Computer Center
Dr. Bhimrao Ambedkar University
IBS, Khandari Campus
Practical file
Object Oriented programming using C++
Submitted to: Sumit Pathak Sir Name: Harsh Tiwari
Teacher’s sign:________________ Father’s Name: Mr.Ram Kumar Tiwari
Class: BCA 2nd Semester
Roll no: 2200555011043
Index
1. Write a program to define the class.
2. Write a program to access the function within the class.
3. Write a program to access the function outside the class.
4. Write a program to use modifiers in c++.
5. Write a program to use modifiers in c++.(program 2)
6. Write a program of default constructor in c++.
7. Write a program of parameterized constructor in c++.
8. Write a program of destructor in c++.
9. Write a program of default constructor using default function.
10. Write a program of single inheritance.
11. Write a program of multiple inheritance.
12. Write a program of multilevel inheritance.
13. Write a program of hierarchical inheritance.
14. Write a program of hybrid inheritance.
15. Write a program of function overloading.
16. Write a program of function overriding.
17. Write a program of operator overloading.
18. Write a program of virtual function.
19. Write a program of constructor in inheritance if the default constructor
present in the base class.
20. Write a program if the default constructor present in the both classes.
21. Write a program when default constructor and parameterized
constructor present in derived class.
22. Write a program when default and parameterized constructor present in
base class.
23. Write a program of explicit convocation when base class parameterized
constructor in derived class.
24. Write a program of inline function.
25. Write a program of manipulators.
26. Write a program of manipulators using the setw, setfill, setprecision
functions.
27. Write a program of writing a file in file handling.
28. Write a program of reading from a file using getline () function.
29. Write a program of reading from a file using get () function.
30. Write a program of exception handling using try, throw and catch.
1. WRITE A PROGRAM TO DEFINE THE CLASS.
#include<iostream>
using namespace std;
class base{
public:
void display(){
cout<<"define class";
};
int main(){
base a;
a.display();
return 0;
// output//
// define class
2. WRITE A PROGRAM TO ACCESS FUNCTION WITH IN THE CLASS.
#include<iostream>
using namespace std;
class base{
public:
int a,b,c;
void sum(){
cout<<"Enter the value of A and B :";
cin>>a>>b;
void sum1(){
c=a+b;
cout<<"Sum of A and B : "<<c;
};
int main(){
base a;
a.sum();
a.sum1();
return 0;
//output
// Enter the value of A and B :20 30
// Sum of A and B : 50
3. WRITE A PROGRAM TO ACCESS FUNCTION OUTSIDE IN THE CLASS.
#include<iostream>
using namespace std;
class base{
public:
int a,b,c;
void sum();
void sum1();
};
int main(){
base a;
a.sum();
a.sum1();
return 0;
void base::sum(){
cout<<"Enter the value of A and B :";
cin>>a>>b;
void base::sum1(){
c=a+b;
cout<<"Sum of A and B : "<<c;
//output
//Enter the value of A and B :20 25
//Sum of A and B : 45
//4.WRITE A PROGRAM TO USE MODIFIERS IN C++.
#include<iostream>
using namespace std;
class Circle
private:
int radius;
public:
void area(int r)
radius =r;
int area = 3.14*radius*radius;
cout << "Radius is: " << radius << endl;
cout << "Area is: " << area;
};
int main()
Circle obj;
obj.area(5);
return 0;
} //output
//Radius is: 6
//Area is: 18
//5. WRITE A PROGRAM TO USE MODIFIERS.(PROGRAM 2)
#include <iostream>
using namespace std;
class Parent
protected:
int a ,b;
};
class Child : public Parent
public:
void setId(int A,int B)
a=A;
b=B;
void displayId()
cout << "A and B: "<<a<<" "<<b<< endl;
};
int main() {
Child obj1;
obj1.setId(34,23);
obj1.displayId();
return 0;
//output
//A and B: 34 23
//CONSTRUCTOR
//6.WRITE A PROGRAM OF DEFAULT CONSTRUCTOR IN C++.
#include <iostream>
using namespace std;
class base
public:
base()
cout<<"default constructor";
};
int main() {
base obj;
return 0;
//output
//default constructor
//7.WRITE A PROGRAM OF PARAMETERIZED CONSTRUCTOR IN C++.
#include <iostream>
using namespace std;
class base
public:
base(int l,int b)
int c;
c=l*b;
cout<<"Area of rectangle :"<<c;
};
int main() {
base obj(20,40);
return 0;
//output
//Area of rectangle :800
//destructor
//8.WRITE A PROGRAM OF DESTRUCTOR IN C++.
#include <iostream>
using namespace std;
class base
public:
base()
{ int c,l,b;
l=10;b=30;
c=l*b;
cout<<"Area of rectangle :"<<c;
~base(){
cout<<"\nHii";
};
int main() {
base t;
return 0;
//output
// Area of rectangle :300
// Hii
//9. WRITE A PROGRAM OF DEFAULT CONSTRUCTOR USING DEFAULT FUNCTION.
#include <iostream>
using namespace std;
class base
public:
base()
cout<<" default constructor.";
void fun(){
cout<<"\n default Function.";
};
int main() {
base t;
t.fun();
return 0;
//output
//default constructor
//default Function
//INHERITANCE
//10.WRITE A PROGRAM OF SINGLE INHERITANCE.
#include <iostream>
using namespace std;
class base
public:
base()
cout<<"Base class.";
};
class derived:public base{
public:
void fun(){
cout<<"\nDerived class.";
};
int main() {
derived t;
t.fun();
return 0;
//output
//Base class.
//Derived class.
//11. WRITE A PROGRAM OF MULTIPLE INHERITANCE.
#include <iostream> // A B
using namespace std; // \ /
class A // C
public:
A()
cout<<"Base class";
};
class B{
public:
void fun1(){
cout<<"class";
};
class C:public A,public B{
public:
void fun2(){
cout<<"\nI am C class";
};
int main() { //output
C t; //Base class //B class //C class
t.fun1();
t.fun2();
return 0;
}
//12.WRITE A PROGRAM OF MULTILEVEL INHERITANCE.
#include <iostream> // A
using namespace std; // |
// B
class base // |
{ public: // C
base()
cout<<"Base class";
};
class derived1:public base{
public:
void fun1(){
cout<<"\nDerived class1";
};
class derived2:public derived1{
public:
void fun2(){
cout<<"\nI am Derived class2";
}; //output
int main() { //Base class //Derived class // Derived2 class2
derived2 t;
t.fun1();
t.fun2();
return 0;
}
//12.WRITE A PROGRAM OF HEIARARICAL INHERITANCE.
#include <iostream>
using namespace std;
class A
{ public:
A()
cout<<"Base class";
};
class B:public A{
public:
void fun1(){
cout<<"\nDerived class1";
};
class C:public A{
public:
void fun2(){
cout<<"\n C CLASS";
};
int main() {
C t;
t.fun2();
return 0;
//output
// Base class //C class
//14.WRITE A PROGRAM OF HYBRID INHERITANCE.
#include <iostream>
using namespace std;
class A
{ public:
A()
cout<<"Base class";
};
class B:public A{
public:
void fun1(){
cout<<"\nDerived class1";
};
class C:public A{
public:
void fun2(){
cout<<"\nC class";
};
class D:public B{
public:
void funD(){
cout<<"\nD class";
};
class E:public D{
public:
void funE(){
cout<<"\nE class";
};
class F:public E{
public:
void funF(){
cout<<"\nF class";
};
class G:public F{
public:
void funG(){
cout<<"\nG class";
};
int main() {
G t;
t.funD();
return 0;
//output
//Base class
// D class
//POLYMORPHISM//
//15. WARITE A PROGRAM OF FUNCTION OVERLOADING.
#include <iostream>
using namespace std;
class base{
public:
void show(){
cout<<"\nhii";
void show(int a){
cout<<"\n hello ";
};
int main(){
base a;
a.show();
a.show(20);
return 0;
//output
//hii
//hello
//15.WRITE A PROGRAM OF FUNCTION OVERRIDING.
#include <iostream>
using namespace std;
class base{
public:
void display(){
cout<<"Overriding"<<endl;
};
class derived:public base{
public:
void display(int a,int b){
int c;
c=a+b;
cout<<"\nSum is "<<c;
};
int main(){
derived d;
//d.display();
d.display(10,20);
return 0;
}
//16.WRITE A PROGRAM OF OPERATOR OVERLOADING.
#include <iostream>
using namespace std;
//sum of complex number
class complex{
private:
int real,img;
public:
void getdata(){
cout<<"Enter the values := ";
cin>>real>>img;
//operator overloading
complex operator +(complex c2){
complex temp;
temp.real=real-c2.real;
temp.img=img-c2.img;
return temp;
void display(){
cout<<"\ncomplex number is :";
cout<<real<<" + "<<img<<"i";
};
int main(){
complex c1,c2,c3;
c1.getdata();
c2.getdata();
c3=c1+c2;
c3.display();
return 0;
//output
// Enter the values := 4 5
//Enter the values := 6 7
//complex number is :10 + 12i
//17.WRITE A PROGRAM OF VIRTUAL FUNCTION.
#include<iostream>
using namespace std;
class base{
public:
virtual void display(){
cout<<"\n base class";
};
class derived:public base{
public:
void display(){
cout<<"\nderived class";
};
int main(){
base *p;
base a;
p=&a;
p->display();
derived d;
p=&d;
p->display();
return 0;
// output
//base class
//derived class
//CONSTRUCTOR IN INHERITANCE//
//18.IF DEFAULT CONSTRUCTOR IS PRESENT IN BASE CLASS
#include<iostream>
using namespace std;
class base{
public:
base(){
cout<<"\nbase class";
};
class derived:public base{
public:
void function(){
cout<<"\nderived class";
};
int main(){
derived d;
d.function();
return 0;
//output
//base class
//derived class
//19.IF DEFAULT CONSTRUCTOR IS PRESENT IN BOTH CLASSES.
#include<iostream>
using namespace std;
class base{
public:
base(){
cout<<"\nconstructor in base class";
};
class derived:public base{
public:
derived(){
cout<<"\nconstructor in derived class";
void function(){
cout<<"\nderived class";
};
int main(){
derived d;
d.function();
return 0;
} //output
//constructor in base class
//constructor in derived class
//20.WHEN DEFAULT CONSTRUCTOR AND PARAMETERIZED CONSTRUCTOR PRESENT IN DERIVED CLASS
#include<iostream>
using namespace std;
class base{
public:
base(){
cout<<"\nconstructor in base class";
};
class derived:public base{
public:
derived(){
cout<<"\nconstructor in derived class";
derived(int a){
cout<<"\nParametrized constructor"<<a;
};
int main(){
derived d(10);
return 0;
//output
//constructor in base class
//Parametrized constructor 10
//21. WHEN DEFAULT CONSTRUCTOR AND PARAMETERIZED CONSTRUCTOR PRESENT IN BASE CLASS.
#include<iostream>
using namespace std;
class base{
public:
base(){
cout<<"\nconstructor in base class";
base(int a){
cout<<"\nparameterized constuctor in base class"<<a;
};
class derived:public base{
public:
derived(){
cout<<"\nconstructor in derived class";
derived(int a){
cout<<"\nParameterized constructor in derived class "<<a;
};
int main(){
derived d(10);
return 0;
//output
//constructor in base class
//Parameterized constructor in base class 10
//22.EXPLCIT CONVOCATION.
// BASE CLASS PARAMETERIZED CONSTRUCTOR IN DERIVED CLASS
#include<iostream>
using namespace std;
class base{
public:
base(){
cout<<"\nbase class constructor";
base(int a){
cout<<"\n base class parameterized constructor"<<a;
};
class derived:public base{
public:
derived(int a):base(a){
cout<<"\nParametrized constructor "<<a;
};
int main(){
derived d(10);
return 0;
//output
//base class parameterized constructor 10
//Parametrized constructor 10
//23.EXPLCIT CONVERSATION
// BASE CLASS PARAMETERIZED CONSTRUCTOR IN DERIVED CLASS DEFAULT CONSTRUCTOR
#include<iostream>
using namespace std;
class base{
public:
base(){
cout<<"\nbase class constructor";
base(int a){
cout<<"\nparameterized consturctor in base class"<<a;
};
class derived:public base{
public:
derived(){
cout<<"\n Hii";
derived(int a):base(a){
cout<<"\nParametrized constructor "<<a;
};
int main(){
derived d(10);
return 0;
} //output
//base parameterized 10
// Parametrized 10
//24. WRITE A PROGRAM OF INLINE FUNCTION.
#include<iostream>
using namespace std;
int cube(int);
int main(){
int k,u;
cout<<"\n Enter the value for cube :";
cin>>k;
u=cube(k);
cout<<"Cube of given value :"<<u;
return 0;
inline int cube( int n){
return n*n*n;
//output
//Enter the value for cube :5
// Cube of given value :125
//MANIPULATORS//
25..setbase(hex,oct,dec)
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n=48;
cout<<"\n Hexa value ="<<hex<<n<<endl;
cout<<" oct value ="<<oct<<n<<endl;
cout<<" Hexa value using satbase ="<<setbase(16)<<n<<endl;
return 0;
//output
//Hexa value =30
// oct value =60
//Hexa value using satbase =30
//26.PROGRAM OF USING setw,setfill,setprecision function.
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int a=48,b=34;
float c=1.234567;
cout<<"Use of setw:\n";
cout<<setw(9)<<a<<endl;
cout<<setw(9)<<b<<endl;
cout<<"Use of setfill:-\n";
cout<<setfill('*')<<a<<endl;
cout<<setfill('#')<<b<<endl;
cout<<"Use of setprecision:-\n";
cout<<setprecision(3)<<c<<endl;
return 0;
//output
//Use of setw:-
48
34
//Use of setfill:-
48
34
//Use of setprecision:-
1.23
//FILE HANDLING//
//27.WRITING TO A FILE
#include<iostream>
#include<fstream>
using namespace std;
int main(){
fstream f;
f.open("abc.txt",ios::out);
if(!f){
cout<<"file is not exist ";
else{
cout<<"new file created";
f<<"File handling";
f.close();
//output
//new file created
//28. READING FROM A FILE(using getline() function)
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
fstream f;
f.open("abc.txt",ios::in);
if(!f){
cout<<"file is not exist ";
else{
string s;
while(getline(f,s)){
cout<<"done\n"<<s;
f.close();
return 0;
//output
//done
//Hii welcome in c++
//29. READING FROM A FILE(using get()function)
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
fstream f;
f.open("abc.txt",ios::in);
if(!f){
cout<<"file is not exist ";
else{
string s;
while(f){
s=f.get();
cout<<s;
f.close();
return 0;
//output
//Hii welcome in c++
//30. PROGFRAM OF EXCEPTION HANDLING(TRY,CATCH,THROW)
#include <iostream>
using namespace std;
int main() {
int num, den, div;
cout << "Enter numerator: ";
cin >> num;
cout << "Enter denominator: ";
cin >> den;
try {
if (den == 0)
throw 0;
div = num/ den;
cout <<num<< " / "<<den<<" = "<<div<<endl;
catch (int n) {
cout << "Error: Cannot divide by " << n << endl;
return 0;
//OUTPUT
//Enter numerator: 1
//Enter denominator: 0
//Error: Cannot divide by 0