Object Oriented Programming
Laboratory
(BTCS304-18)
[Link] (AI&DS) 3rd
(ODD)
CT Institute of Technology &
Research
Submitted To: Submitted by:
Ms. Mamta Devi Name: Arti
Class: B Tech (AI&DS)
Roll No:2322657
PRACTICAL NO :1
Task 1. Write a program that uses a class where the member function are
defined inside a class.
Source code:
#include<iostream>
using namespace std; class car
{
private:
char car_name[10];
int car_model;
public:
void getdata()
{
cout<<"enter the car name:";
cin>> car_name;
cout<<"enter the car model:";
cin>> car_model;
}
void showdata()
{
cout<<"car_name is:"<<car_name<<endl;
cout<<"car_model is:"<<car_model;
}
};
int main()
{
car Mycar;
[Link]();
[Link]();
return 0;
}
Output:
enter the car name:BMW
enter the car model:6574
car_name is:BMW
car_model is:6574
PRACTICAL NO: 2
Task 12. Write a program that uses a class where the member function are
defined outside a class.
Source code:
#include<iostream>
using namespace std; class car
{
private:
char car_name[10];
int car_model;
public:
void getdata();
void showdata();
};
void car::getdata()
{
cout<<"enter the car name:";
cin>> car_name;
cout<<"enter the car model:";
cin>> car_model;
}
void car::showdata()
{
cout<<"car_name is:"<<car_name<<endl;
cout<<"car_model is:"<<car_model;
}
int main()
{
car Mycar;
[Link]();
[Link](); return 0;
}
OUTPUT: -
enter the car name:BMW
enter the car model:5674
car_name is:BMW
car_model is:5674
PRACTICAL NO: 3
Task 3. Write a program to demonstrate the use of static data member.
Source code:
#include <iostream>
using namespace std;
class A {
public:
static int x;
A() { cout << "A's constructor called " << endl; }
};
int A::x = 2;
int main()
{
cout << "Accessing static data member: " << A::x
<< endl;
return 0;
}
Output:
Accessing static data member: 2
PRACTICAL NO: 4
Task 4. Write a program to demonstrate the use of construct data member.
Source code:
#include<iostream>
using namespace std;
class demo
int value ;
public:
demo(int x=0)
value=x;
int getvalue()const{
return value;
};
int main()
const demo d(20);
demo d1(8);
cout<<"the value using object d:"<<[Link]();
cout<<"the value using object d1:"<<[Link]();
return 0;
Output:
The value using obect d:20th value using object d1:8
PRACTICAL NO: 5
Task 5. Write a program to demonstrate the use of zero argument and
parameterized constructors.
Source code:
#include <iostream>
using namespace std;
class Demo
private:
int A;
int B;
int C;
public:
Demo();
void set(int A, int B, int C);
void print();
};
Demo::Demo()
A = 1;
B = 1;
C = 1;
void Demo::set(int A, int B, int C)
this->A = A;
this->B = B;
this->C = C;
void Demo::print()
cout << "Value of A : " << A << endl;
cout << "Value of B : " << B << endl;
cout << "Value of C : " << C << endl;
int main()
Demo obj = Demo();
[Link]();
[Link](10, 20, 30);
[Link]();
return 0;
Output:
Value of A : 1
Value of B : 1
Value of C : 1
Value of A : 10
Value of B : 20
Value of C : 30
PRACTICAL NO: 6
Task 6. Write a program to demonstrate the use of single inheritance.
Source code:
#include <iostream>
using namespace std;
class fruit{
public:
void eat(){
cout<<"YOU CAN EAT ME"<<endl; }
void energy(){
cout<<"I GIVE LOTS OF ENERGY"<<endl; } };
class orange:public fruit{
public:
void main(){
cout<<"I AM AN ORANGE"<<endl; }
void peel(){
cout<<"YOU HAVE TO PEEL ME"<<endl; } };
int main(){
orange orange1;
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
}
Output:
PRACTICAL NO: 7
Task 7. Write a program to demonstrate the use of multiple inheritance.
Source code:
#include <iostream>
using namespace std;
class mother{
public:
void Name1(){
cout<<"Name of mother is SUSHMA"<<endl;
void Age1(){
cout<<"Age of mother is 45"<<endl;
};
class father{
public:
void Name2(){
cout<<"Name of father is YASH"<<endl;
void Age2(){
cout<<"Age of father is 50"<<endl;
};
class child:public mother, public father{
public:
void Name(){
cout<<"Name of child is ANAYA"<<endl;
}
void Age(){
cout<<"Age of child is 18"<<endl;
};
int main(){
child c1;
c1.Name1();
c1.Age1();
c1.Name2();
c1.Age2();
[Link]();
[Link]();
return 0;
Output:
PRACTICAL NO: 8
Task 8. Write a program to demonstrate the use of dynamic constructor.
Source code:
#include <iostream>
using namespace std;
class geeks {
const char* p;
public:
geeks()
p = new char[6];
p = "geeks";
void display() { cout << p << endl; }
};
int main()
geeks obj;
[Link]();
Output:
geeks
PRACTICAL NO: 9
Task 9. Write a program to demonstrate the use of explicit constructor.
Source code:
#include<iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0.0,
double i = 0.0) : real(r), imag(i)
bool operator == (Complex rhs)
return (real == [Link] &&
imag == [Link]);
};
int main()
Complex com1(3.0, 0.0);
if (com1 == 3.0)
cout << "Same";
else
cout << "Not Same";
return 0;
Output:
same
PRACTICAL NO: 10
Task 10. Write a program to demonstrate the use of initializer list.
Source code:
#include <iostream>
using namespace std;
class point{
private:
int x;
int y;
public:
point(int i=0,int j=0){
x=i;
y=j;
int getx(){return x;}
int gety(){return y;}
};
int main()
point t1(18,45);
cout<<"x="<<[Link]();
cout<<",y="<<[Link]();
return 0;
Output:
PRACTICAL NO: 11
11(a) Write a program to demonstrate of prefix increment operator.
Source code:
#include <iostream>
using namespace std;
class integer{
private:
int i;
public:
integer(int i=0){ this->i=i; }
integer&operator++()
{ ++i;
return*this; }
void display()
{ cout<<"i="<<i<<endl; }
};
int main(){
integer i1(3); cout<<"before increament";
[Link]();
integer i2=++i1;
cout<<"after pre increament"; cout<<"i1";
[Link](); cout<<"i2";
[Link]();
return 0;
}
Output:
11(b) Write a program to demonstrate the postfix increment.
Source code:
#include <iostream>
using namespace std;
class Integer {
private:
int i;
public:
Integer(int i = 0){
this->i = i;}
Integer operator++(int){
Integer temp = *this;
++i;
return temp;}
void display(){cout << "i = " << i << endl;}
};
int main()
Integer i1(3);cout << "Before increment: ";
[Link]();
Integer i2 = i1++;cout << "After post increment: " << endl;
cout << "i1: ";[Link]();
cout << "i2: "; [Link]();
return 0;
Output:
11(c) Write a program to demonstrate the prefix decrement.
Source code:
#include <iostream>
using namespace std;
class integer{
private:
int i;
public:
integer(int i=0){ this->i=i; }
integer&operator--()
{ --i;
return*this; }
void display()
{ cout<<"i="<<i<<endl; }
};
int main(){
integer i1(3); cout<<"before decreament";
[Link]();
integer i2=--i1;
cout<<"after pre decreament"; cout<<"i1";
[Link](); cout<<"i2";
[Link]();
return 0;
Output:
11(d)Write a program to demonstrate the postfix decrement.
Source code:
#include <iostream>
using namespace std;
class Integer {
private:
int i;
public:
Integer(int i = 0){
this->i = i;}
Integer operator--(int){
Integer temp = *this;
--i;
return temp;}
void display(){cout << "i = " << i << endl;}
};
int main()
{
Integer i1(3);cout << "Before decrement: ";
[Link]();
Integer i2 = i1--;cout << "After post decrement: " << endl;
cout << "i1: ";[Link]();
cout << "i2: "; [Link]();
return 0;
Output:
PRACTICAL NO :12
Task 12. write a program to demonstrate the conversion of class type to
another type class by using constructor.
Source code:
#include<iostream>
using namespace std;
class CGS {
int mts;
int cms;
public:
void showdata(){
cout<<"Meters and centimeters in CGS system:";
std::cout << mts<<" meters "<<cms<<" centimeters" << std::endl;}
CGS(int x,int y) {
mts=x;cms=y;
int getcms(){
return cms;
int getmts(){
return mts;}
};
class FPS{
int feet;
int inches;
public:
FPS(){
feet=0;
inches=0;
FPS(CGS d2){
int x;
x=[Link]()+[Link]()*100;
x=x/2.5;
feet=x/12;
inches=x%12;}
void showdata(){
cout<<"feet and inches in FPS system:";
std::cout << feet<<" feet "<<inches<<" inches" << std::endl;}
};
int main(){
CGS d1(9,10);
FPS d2;
d2=d1;
[Link]();
[Link]();
return 0;
Output:
PRACTICAL NO :13
Task 13. Write a program to demonstrate the runtime polymorphism.
Source code:
#include <iostream>
using namespace std;
class Shape {
public:
virtual void calculate(){
cout << "Area of your Shape ";
virtual ~Shape(){
cout << "Shape Destuctor Call\n";}
};
class Rectangle : public Shape {
public:
int width, height, area;
void calculate(){
cout << "Enter Width of Rectangle: ";cin >> width;
cout << "Enter Height of Rectangle: ";cin >> height;
area = height * width;
cout << "Area of Rectangle: " << area << "\n";}
virtual ~Rectangle(){
cout << "Rectangle Destuctor Call\n";}
};
class Square : public Shape {
public:
int side, area;
void calculate(){
cout << "Enter one side your of Square: ";cin >> side;
area = side * side;
cout << "Area of Square: " << area << "\n";}
virtual ~Square({
cout << "Square Destuctor Call\n";}
};
int main(){
Shape* S;
Rectangle r;
S = &r;
S->calculate();
Square sq;
S = &sq;
S->calculate();
return 0;
Output:
PRACTICAL NO: 14
Task 14. Write a program to demonstrate the exception handling.
Source code:
#include <iostream>
using namespace std;
int main(){
try {
int numerator = 10;
int denominator = 0;
int res;
if (denominator == 0) {
throw runtime_error(
"Division by zero not allowed!"); }
res = numerator / denominator;
cout << "Result after division: " << res << endl;
catch (const exception& e) {
cout << "Exception " << [Link]() << endl;
return 0;
Output:
PRACTICAL NO :15
Task 15. Write a program to demonstrate the use of class template.
Source code:
#include <iostream>
using namespace std;
template <typename T> class Array {
private:
T* ptr;
int size;
public:
Array(T arr[], int s);
void print();};
template <typename T> Array<T>::Array(T arr[], int s){
ptr = new T[s];
size = s;
for (int i = 0; i < size; i++)
ptr[i] = arr[i];}
template <typename T> void Array<T>::print(){
for (int i = 0; i < size; i++)
cout << " " << *(ptr + i); cout << endl;}
int main(){
int arr[5] = { 1, 2, 3, 4, 5 };
Array<int> a(arr, 5);
[Link]();
return 0;
}
Output: