1903107
1903107
Department of Computer Science & Engineering
Practical File
SUBJECT: OBJECT ORIENTED PROGRAMMING LAB
[BTCS304-18]
B.Tech – III Semester
[Batch 2019-23]
CGC College of Engineering Landran,
Mohali-140307
Submitted To: Mr. Gurbaz Singh
(ASSISTANT PROFESSOR)
Submitted By:Arshpreet Singh,
1903107,
B.tec CSE 3Z(Z1)
1903107
Table of Contents
S. No. Aim of Practical
Write a program that uses a
1 class where the member
functions are defined inside a
class.
Write a program that uses a
2 class where the member
functions are defined outside
a class.
Write a program to
3 demonstrate the use of static
data members.
Write a program to
4 demonstrate the use of const
data members.
Write a program to
5 demonstrate the use of
zero argument and
parameterized
constructors.
Write a program to
6 demonstrate the use of
dynamic constructor.
Write a program to
7 demonstrate the use of explicit
constructor.
Write a program to
8 demonstrate the use of
initializer list.
Write a program to
9 demonstrate the overloading
of increment and decrement
operators.
Write a program to
10 demonstrate the overloading
of memory management
operators.
Write a program to
11 demonstrate the typecasting
of basic type to class type.
Write a program to
12 demonstrate the typecasting
of class type to basic type.
Write a program to
13 demonstrate the typecasting
1903107
of class type to class type.
Write a program to
14 demonstrate the multiple
inheritances.
Write a program to
15 demonstrate the runtime
polymorphism.
Write a program to
16 demonstrate the exception
handling.
Write a program to
17 demonstrate the use of class
template.
Write a program to
18 demonstrate the reading and
writing of mixed type of
data.
Program 1: Write a program that uses class where member functions are defined
inside a class?
1903107
Answer- 1.
#include <iostream>
using namespace std;
class car
{
private:
int car number;
char car model[10];
public:
void getdata()
{ cout<<"Enter car number: ";
cin>>car_number;
cout<<"\n Enter car model: ";
cin>>car_model; }
void showdata()
{ cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
} };
int main()
{ car c1;
c1.getdata();
c1.showdata();
return 0; }
Output-
Program 2: Write a program that uses class where member functions are defined
outside a class?
Answer2:
1903107
#include <iostream>
using namespace std;
class car
{ int car_number;
char car_model[10];
public:
void getdata();
void showdata(); };
void car::getdata()
{ cout<<"Enter car number: ";
cin>>car_number;
cout<<"\n Enter car model: ";
cin>>car_model; }
void car::showdata()
{ cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model; }
int main()
{ car c1; c1.getdata(); c1.showdata();
return 0;
}
Output:
Program 3: Write a program to demonstrate the use of
static data members?
Answers:
#include <iostream>
1903107
#include<string.h>
using namespace std;
class test
{ int b;
static int a;
public:
void getdata()
{ cout<<"enter b value"<<endl;
cin>>b;
a++; }
void putdata()
{ cout<<"b= "<<b<<endl;
cout<<"count= "<<a<<endl; } };
int test:: a;
int main()
{ test t1,t2;
t1.getdata(); t2.getdata();
t1.putdata();
t2.putdata();
return 0;
}
Output:
Program 4: Write a program to use a constant data
member?
Solution: #include<iostream>
using namespace std;
1903107
int main()
{ const double a=4.245;
cout<<a<<endl;
//a=3.445;
//a=a+10;
cout<<a<<endl;
return 0; }
Output:
(ii) #include<iostream>
using namespace std;
int main()
{
int x=1; int const *p=&x;
cout<<*p<<endl;
x=x+10;
// *p=*p+10;
cout<<*p<<endl;
return 0; }
Output:
Program 5: Write a program to demonstrate the use of
zero argument and parametrized constructors?
Solution:
#include <iostream>
1903107
using namespace std;
class XYZ
{
public:
int x,y;
XYZ()
{
x=10;
y=20;
}
};
int main()
{
XYZ c;
cout<<"x: "<<c.x<<endl;
cout<<"y: "<<c.y<<endl;
return 0;
}
Output:
Program 6: Write a program to demonstrate the use of dynamic constructor?
Solution: #include<iostream>
using namespace std;
class ABC
1903107
{ int * p;
public:
ABC()
{ p=new int;
*p=20; }
ABC(int x)
{ p=new int;
*p=x; }
int show()
{
return(*p);
}
};
int main()
{
ABC obj, obj1(8);
cout<<"The value of obj's is: "<<obj.show()<<endl;
cout<<"The value of obj1's is: "<<obj1.show()<<endl;
return 0;
}
Output:
Program 7: Write a program to demonstrate the use of explicit constructor?
Solution: #include <iostream>
using namespace std;
1903107
class imp
public:
imp (int k)
cout<<"implict constructor: "<<k<<endl;
};
int main()
imp obj1=18;
imp obj2= (4);
imp obj3=(imp)54;
exp obj4=(exp)87;
exp obj5(43);}
Output:
Program 8: Write a program to demonstrate the use of initializer list?
Solution:
#include<iostream>
1903107
using namespace std;
class base
{
int i;
public:
base (int x)
{ i=x;
cout<<"BASE CLASS i: "<<i<<endl;
} };
class derived: public base
{ int j;
public:
derived(int x, int y):base(y)
{ j=x;
cout<<"DERIVED CLASS j: "<<j<<endl; }
};
int main ()
{
derived d(5,10);
return 0;
}
Output:
Ques 9: Write a program to demonstrate the overloading of increment and
decrement operators?
Solution: #include <iostream>
1903107
using namespace std;
class ABC
{ Private:
int x;
public:
ABC()
{ x=5; }
void operator ++ ()
{ x = x+5; }
void operator -- ()
{ x = x-3; }
void display()
cout<<"x is"<<x;
};
int main()
Output:
Program 10: write a program to demonstrate the overloading of memory
mangement operators?
Solution: #include<stdio.h>
1903107
#include<stdlib.h>
using namespace std;
class student
string name;
int age;
public:
student()
cout<< "Constructor is called\n" ;
student(string name, int age)
this->name = name;
this->age = age;
void display()
cout<< "Name:" << name << endl;
cout<< "Age:" << age << endl;
void * operator new(size_t size)
cout<< "Overloading new operator with size: " << size << endl;
Ques 11: Write a program to demonstrate the typecasting of basic type to
class type?
Solution: #include<iostream>
1903107
using namespace std;
class data
int x;
float y;
public:
data()
x=0;
y=0;
data(float m)
x=2;
y=m;
void show() OUTPUT
{
cout<<"x is: "<<x<<endl;
cout<<"y is: "<<y<<endl;
};
int main ()
Ques 12: Write a program to demonstrate the typecasting of class type to basic
type?
Solution: #include <iostream>
1903107
using namespace std;
class ABC
int k;
float z;
public:
ABC()
k=0;
z=0;
operator int ()
return k;
operator float()
return z;
ABC(float p)
k=2;
z=p;
void show()
1903107
cout<<"object values : "<<k<<endl;
cout<<"object values : "<<z<<endl;
};
int main()
int x;
float f;
ABC obj;
obj=8.9;
obj.show();
x =obj;
f =obj;
cout<<"value of x: "<<x<<endl;
cout<<"value of f: "<<f<<endl;
return 0;
Output:
Ques 13: Write a program to demonstrate the typecasting of class type to class
type?
Solution: #include <iostream>
1903107
#include<string.h>
using namespace std;
class type_one
string a= "rishika";
public:
string get_string()
return (a);
void display()
cout<<a<<endl; OUTPUT
}
};
class type_two
string b;
public:
void operator=(type_one a)
return 0;
Program 14: Write a program to demonstrate the
multiple Inheritance?
Solution: #include <iostream>
1903107
using namespace std;
class A {
public:
int x;
void getx()
cout<<"enter the value of x: ";
cin>>x;
};
class B {
public:
int y;
void gety()
cout<<"enter the value of y ";
cin>>y;
};
class C : public A, public B
public:
void sub()
cout<<"sub = "<<x-y;
} ;
1903107
int main()
C obj;
obj.getx();
obj.gety();
obj.sub();
return 0;
Output:
Program 15: Write a program to demonstrate the runtime
polymorphism?
Solution: include<iostream>
using namespace std;
class B
public:
virtual void Display(void)
cout << "\n The member function Display( ) " ;
cout << "of the \"Base Class B\" is invoked \n"
;
};
class D1 : public B
public:
1903107
void Display(void)
cout << "\n The member function Display( ) " ;
cout <<"of the \"Derived Class D1\" is invoked \n" ;
};
class D2 : public B
public:
void Display(void)
cout << "\n The member function Display() " ;
cout << "of the \"Derived Class D2\" is invoked " ;
};
int main()
B* base_ptr ;
D1 der1_obj ; OUTPUT
base_ptr = &der1_obj ;
base_ptr->Display( );
D2 der2_obj ;
base_ptr = &der2_obj ;
base_ptr->Display( );
return 0;
1903107
Output:
Program 16: Write a program to demonstrate the
exception handling?
Solution: #include <iostream>
using namespace std;
int main()
int x = -1;
// Some code OUTPUT
cout << "Before try \n";
try {
cout << "Inside try \n";
if (x < 0)
throw x;
cout << "After throw (Never executed) \n";
catch (int x ) {
return0;