Bca 3
Bca 3
Assignment
Of
Object Oriented
Programming Using 'C++'
Submitted To:
Mr. Vivek Sir Submitted By:
Utkarsh Jindal
BCA 3rd Sem
Page |2
Acknowledgement
I would like to express my special thanks of
gratitude to my teacher Mr. VIVEK SIR who
gave me the golden opportunity to do this
wonderful project on the topic OBJECT
ORIENTED PROGRAMMING USING 'C++' ,
which also helped me in doing a lot of Research
and i came to know about so many new things I
am really thankful to them.
Secondly i would also like to thank my parents
and friends who helped me a lot in finalizing this
project within the limited time frame.
Page |3
INDEX
PROGRAM PAGE NO.
Demo Class 4
Default Argument 6
Function Overloading 7
Inline Function 9
Static Data Member & Functions 10
Friend Function 12
Unary Operator Overloading 14
Binary Operator Overloading 16
Constructor 19
Virtual Function 21
Exceptional Handling 23
Namespace 25
Manipulators 27
Inheritance 28
Generic Function 31
Generic Classes 32
Writing in a file 34
Read & Display of a File 35
Page |4
class demo
{
int rn;
char name[15];
public:
void input()
{
cout<<"Enter name of student:";
cin>>name;
cout<<"Enter roll number of student:";
cin>>rn;
}
void display()
{
cout<<endl<<"Name of student="<<name<<endl;
cout<<"Roll number of student="<<rn<<endl;
}
};
int main()
{
demo obj;
obj.input();
Page |5
obj.display();
return 0;
}
Output:-
Page |6
Output:-
Page |7
#define PI=3.14
void area(float,float);
void area(int);
void area(float);
int main()
{
float l,b,r;
int s;
cout<<"Enter the values of length and breath of rectangle:"<<endl;
cin>>l>>b;
cout<<"Enter the value of radius of circle:"<<endl;
cin>>r;
cout<<"Enter the value of side of square:"<<endl;
cin>>s;
area(l,b);
area(r);
area(s);
return 0;
}
void area(float l,float b)
{
cout<<"The area of rectangle="<<(l+b)<<endl;
}
Page |8
void area(float r)
{
cout<<"The area of circle="<<(3.14*r*r)<<endl;
}
void area(int s)
{
cout<<"The area of square="<<(s*s)<<endl;
}
Output:-
Page |9
Output:-
P a g e | 10
class a
{
static int count;
public:
void get(void)
{
count++;
}
static void put(void)
{
cout<<"Count="<<count<<endl;
}
};
int a::count=10;
int main()
{
a t1,t2;
t1.get();
P a g e | 11
t2.get();
a::put();
a t3;
t3.get();
a::put();
return 0;
}
Output:-
P a g e | 12
class rect2;
class rect1
{
int l,b;
public:
void get(int x,int y)
{
l=x;
b=y;
}
friend void add(rect1 r,rect2 s);
};
class rect2
{
int l,b;
public:
void get(int p,int q)
{
l=p;
b=q;
}
friend void add(rect1 r,rect2 s);
P a g e | 13
};
void add(rect1 r,rect2 s)
{
cout<<" Sum of lengths of rectangle are: "<<r.l+s.l<<endl;
cout<<" Sum of breath of rectangle are: "<<r.b+s.b<<endl;
}
int main()
{
rect1 obj;
obj.get(5,8);
rect2 obj1;
obj1.get(4,7);
add(obj,obj1);
return 0;
}
Output:-
P a g e | 14
class oper
{
int num;
public:
void input()
{
cout<<" Enter value:";
cin>>num;
}
void operator-()
{
num=-num;
}
void display()
{
cout<<" Value = "<<num<<endl;
}
};
int main()
{
oper abc;
abc.input();
P a g e | 15
-abc;
abc.display();
return 0;
}
Output:-
P a g e | 16
class Complex
{
private:
float real;
float imag;
public:
Complex(): real(0), imag(0){ }
void input()
{
cout << " Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
// Operator overloading
Complex operator - (Complex c2)
{
Complex temp;
temp.real = real - c2.real;
P a g e | 17
return temp;
}
void output()
{
if(imag < 0)
cout << " Output Complex number: "<< real << imag << "i"<<endl;
else
cout << " Output Complex number: " << real << "+" << imag <<
"i"<<endl;
}
};
int main()
{
Complex c1, c2, result;
result.output();
return 0;
}
Output:-
P a g e | 19
class box
{
float width,height;
public:
box()
{
width=9.8;
height=8.9;
}
box(float w,float h)
{
width=w;
height=h;
}
box(box &b)
{
width=b.width;
height=b.height;
}
void area(box b);
};
void box::area(box b)
P a g e | 20
{
float a;
a=(b.width*b.height);
cout<<"Constructor call automatically after object created "<<endl;
cout<<"area="<<a<<endl;
}
int main()
{
box b1(5.6,6.5);//p call
box b2(b1);
b2.area(b1);
return 0;
}
Output:-
P a g e | 21
class base
{
public:
virtual void display()
{
cout<<endl<<" D.S."<<endl;
}
void show()
{
cout<<endl<<" Show derived"<<endl;
}
};
class derived:public base
{
public:
void display()
{
cout<<endl<<" Display derived"<<endl;
}
void show()
{
cout<<endl<<"Show derived";
}
P a g e | 22
};
int main()
{
base *ptr;
base b;
derived d;
ptr=&b;
ptr->display();
ptr->show();
ptr=&d;
ptr->display();
ptr->show();
return 0;
}
Output:-
P a g e | 23
int main()
{
int a,b;
cout << " Enter 2 numbers: ";
cin >> a >> b;
try
{
if (b != 0)
{
float div = (float)a/b;
if (div < 0)
throw 'e';
cout << " a/b = " << div;
}
else
throw b;
}
catch (int e)
{
cout << " Exception: Division by zero"<<endl;
}
P a g e | 24
Output:-
P a g e | 25
Output:-
P a g e | 27
int main()
{
int value;
cout<<"Enter number"<<endl;
cin>>value;
cout<<"Decimal base="<<dec<<value<<endl;
cout<<"Hexadecimal base="<<hex<<value<<endl;
cout<<"Octal base="<<oct<<value<<endl;
return 0;
}
Output:-
P a g e | 28
Program of Inheritance:-
#include <iostream>
using namespace std;
class Person
{
public:
string profession;
int age;
{
public:
void teachMaths() { cout << "I can teach Maths." << endl; }
};
// Footballer class is derived from base class Person.
class Footballer : public Person
{
public:
void playFootball() { cout << "I can play Football." << endl; }
};
int main()
{
MathsTeacher teacher;
teacher.profession = "Teacher";
teacher.age = 23;
teacher.display();
teacher.teachMaths();
Footballer footballer;
footballer.profession = "Footballer";
footballer.age = 19;
footballer.display();
footballer.playFootball();
return 0;
}
P a g e | 30
Output:-
P a g e | 31
Output:-
P a g e | 32
};
int main()
{
s <int,int>s1;
s1.input();
s1.output();
return 0;
}
Output:-
P a g e | 34
char text[200];
fstream file;
file.open ("example.txt", ios::out | ios::in );
file.close();
return 0;
}
Output:-
P a g e | 35
#include<fstream>
int main()
{
ifstream fin;
fin.open("c:/abc.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
cout<<endl;
fin.close();
return 0;
}
Output:-