Oops Project 29
Oops Project 29
1903107
Department of Computer Science & Engineering
Practical File
[BTCS304-18]
[Batch 2019-23]
Table of Contents
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:
(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:
};
int main()
{
XYZ c;
cout<<"x: "<<c.x<<endl;
cout<<"y: "<<c.y<<endl;
return 0;
}
Output:
class imp
public:
imp (int k)
};
int main()
imp obj1=18;
imp obj3=(imp)54;
exp obj4=(exp)87;
exp obj5(43);}
Output:
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:
Solution: #include<stdio.h>
1903107
#include<stdlib.h>
class student
string name;
int age;
public:
student()
this->name = name;
this->age = age;
void display()
cout<< "Overloading new operator with size: " << size << endl;
Solution: #include<iostream>
1903107
class data
int x;
float y;
public:
data()
x=0;
y=0;
data(float m)
x=2;
y=m;
};
int main ()
Ques 12: Write a program to demonstrate the typecasting of class type to basic
type?
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
};
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?
#include<string.h>
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;
class A {
public:
int x;
void getx()
cin>>x;
};
class B {
public:
int y;
void gety()
cin>>y;
};
public:
void sub()
cout<<"sub = "<<x-y;
} ;
1903107
int main()
C obj;
obj.getx();
obj.gety();
obj.sub();
return 0;
Output:
Solution: include<iostream>
class B
public:
};
class D1 : public B
public:
1903107
void Display(void)
};
class D2 : public B
public:
void Display(void)
};
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:
int main()
int x = -1;
try {
if (x < 0)
throw x;
catch (int x ) {
return0;