OOPS Lab File (4) Edited Saurabh
OOPS Lab File (4) Edited Saurabh
OUTPUT
PROGRAM NO-6
AIM - Create a base class called shape. Use this class to store two double type values that
could be used to compute the area of figures. Derive two specific classes called triangle and
rectangle from the base shape. And the base class, a member function get_data() to
initialize base class data members and another member function display_area() to compute
and display the area of figures. Make display area() as a virtual function and define this
function in the derived classes to suit their requirements. Using these three classes, design a
program that will accept dimensions of a triangle or a rectangle interactively, and display
the area. Remember the two values given as input will be treated as lengths of two sides in
the case of rectangles, and as base and height in the case of triangles, and used as follows:
area of rectangle = x*y, area of triangle = ½x*y.
#include <iostream>
using namespace std;
class Shape {
protected: double l,
b; public:
void getdata(double l, double b) { this-
>l = l; this->b = b;
}
void virtual display_area() = 0;
};
class triangle : public Shape { public:
void display_area() {
cout << "\nArea of Triangle:" << (0.5 * l * b);
} };
class Rectangle : public Shape { public:
void display_area() {
cout << "\nArea of Rectangle:" << (l * b);
}
}; int
main(void) {
Shape *s1; triangle t1; Rectangle r1; s1 = &t1;
s1->getdata(30, 20); s1->display_area(); s1 = &r1;
s1->getdata(6, 5); s1->display_area(); return 0;
}
OUTPUT
PROGRAM NO-7
AIM - Sample program of class.
#include <iostream>
using namespace std;
class item {
private:
int number; float cost; public:
void getdata(int a, float b) { number
= a; cost = b;
} void putdata() { cout <<
"Number:" << number<<endl; cout
<< "Cost:" << cost;
} }; int
main(void) {
item x;
x.getdata(14, 2.5);
x.putdata(); return
0;
}
OUTPUT
PROGRAM NO-8
AIM - Sample program of friend function.
#include <iostream> using
namespace std; class
sample { int a, b; friend
void print(sample);
}; void print(sample
s) {
s.a = 10;
s.b = 20; cout << "a="
<< s.a; cout << "\nb="
<< s.b;
} int main(void)
{ sample s;
print(s); return
0;
}
OUTPUT
PROGRAM NO-9
AIM - Sample program of static data members.
#include <iostream>
using namespace std;
class test { public:
static int a; void
geta() { cout <<
"\na" << a;
} }; int test::a =
10; int main(void) {
test::a = 100; test
t; cout << "\na" <<
t.a;
t.geta(); return
0;
}
OUTPUT
PROGRAM NO-10
AIM - Program to illustrate the implementation of static member functions.
#include <iostream>
using namespace std;
class test { int
code; static int
count; public:
void setcode(){ code
= ++count;} void
showcode(){
cout << "Object number:" << code << "\n";}
static void showcount(){ cout << "count:"
<< count << "\n";
} }; int
test::count; int
main(void){ test
t1, t2;
t1.setcode();
t2.setcode();
test::showcount()
; test t3;
t3.setcode();
test::showcount()
; t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
OUTPUT
PROGRAM NO-11
AIM - Program to illustrate the implementation of system defined default constructor.
#include <iostream> using
namespace std; class test {
int a, b; public: void get()
{ a = 10; b = 20; } void
show() { cout << "a = " << a
<< "\n"; cout << "b = " << b
<< "\n";
} }; int
main(void) {
test t;
t.get();
t.show(); return
0;
}
OUTPUT
PROGRAM NO-12
AIM - Program to illustrate the implementation of parameterized constructor.
#include <iostream> using
namespace std; class integer
{ int m, n; public:
integer(int x, int y) { m =
x; n = y; } void display() {
cout << "m = " << m << "\n";
cout << "n = " << n << "\n";
} }; int main(void) { integer
int1(0, 100); integer int2 =
integer(10, 20); cout <<
"Object 1 \n"; int1.display();
cout << "Object 2 \n";
int2.display(); return 0;
}
OUTPUT
PROGRAM NO-13
AIM - Program to illustrate the implementation of copy constructor.
#include <iostream>
using namespace std;
class code { int id;
public: code(int a)
{ id = a; }
code(code &x) { id =
x.id; } void
display(void) { cout
<< id << "\n";
} }; int main(void)
{ code A(100); code
B(A); code C = A;
cout << "id of A :";
A.display(); cout <<
"id of B :";
B.display(); return
0;
}
OUTPUT
PROGRAM NO-14
AIM - Program to illustrate the implementation of destructors.
#include <iostream>
using namespace std;
int count; class
alpha { public:
alpha() { count++;
cout << "Object created " << count << endl;
} ~alpha()
{
cout << "Object destroyed " << count << endl;
count--; }}; int main(void) { cout << "Main
Block" << endl; alpha A1, A2, A3, A4;
{
cout << "Inner block" << endl;
alpha A5; cout << "Exit" <<
endl;}
{
cout << "Inner block 2" << endl;
alpha A6; cout << "Exit" << endl;}
cout << "Main block exit" << endl;
return 0;}
OUTPUT
PROGRAM NO-15(a)
AIM - Program to illustrate single inheritance in public mode.
#include <iostream>
using namespace std;
class B { int a;
public: int b; void
get_ab(); int
get_a(void); void
show_a(); }; class D
: public B { int c;
public:
void mul(void); void
display(void);
}; void B::get_ab(void) {
a = 5; b = 10; } int
B::get_a() { return a; }
void B::show_a() { cout <<
"a:" << a << endl;
} void D::mul()
{ c = b *
get_a();
} void D::display()
{ cout << "a=" <<
get_a() << endl;
cout << "b=" << b
<< endl; cout <<
"c=" << c << endl;
} int main(void)
{
D d;
d.get_ab(); d.mul();
d.show_a(); d.display();
d.b = 20;
d.mul();
d.display();
return 0; }
OUTPUT
PROGRAM NO-15(b)
AIM - Program to illustrate single inheritance in private mode.
#include <iostream>
using namespace std;
class student { int
id; char name[10];
public:
void getstu() { cout <<
"Enter ID" << endl; cin >>
id;
cout << "Enter the name" << endl;
cin >> name; } void putstu() {
cout << "ID " << id << endl; cout
<< "Name " << name << endl;
} };
class phy : private student {
float height, weight; public:
void getphy() {
cout << "Enter the height in cm" << endl;
cin >> height;
cout << "Enter the weight in Kg" << endl;
cin >> weight; getstu(); } void putphy()
{
cout << "Height " << height << endl;
cout << "Weight " << weight << endl;
putstu(); } }; int main(void) { phy
p;
p.getphy();
p.putphy();
return 0; }
OUTPUT
PROGRAM NO-16
AIM - Program to illustrate multi-level inheritance.
#include <iostream>
using namespace std;
class student {
protected: int
rollNo; public:
void get_number(int); void
put_number(void);
};
void student::get_number(int a) {
rollNo = a; }
void student::put_number(void) { cout <<
"Roll Number:" << rollNo << endl;
}
class test : public student {
protected: float subject1;
float subject2; public:
void get_marks(float, float); void
put_marks(void);
};
void test::get_marks(float a, float b) {
subject1 = a; subject2 = b; }
void test::put_marks(void) {
cout << "Marks in subject 1:" << subject1 << endl; cout << "Marks in subject
2:" << subject2 << endl;
}
class result : public test {
float total; public:
void display() { total =
subject1 + subject2;
put_number(); put_marks();
cout << "Total=" << total << endl;
} }; int main(void) {
result r1;
r1.get_number(54);
r1.get_marks(50, 30);
r1.display(); return
0;
}
OUTPUT
PROGRAM NO-17
AIM - Program to illustrate multiple inheritance.
#include <iostream>
using namespace std;
class student { int
id; char name[30];
public:
void getstu() { cout <<
"Enter ID" << endl; cin >>
id;
cout << "Enter the name" << endl;
cin >> name; } void putstu() {
cout << "ID " << id << endl; cout
<< "Name " << name << endl;
} }; class
marks {
protected: int
m1, m2, m3;
public:
void getmarks() { cout << "Enter
marks: " << endl; cout << "M1:"
<< endl; cin >> m1; cout << "M2:"
<< endl; cin >> m2; cout << "M3:"
<< endl; cin >> m3;
} void putmarks() { cout <<
"M1:" << m1 << endl; cout <<
"M2:" << m2 << endl; cout <<
"M3:" << m3 << endl;
} };
class result : public student, public marks {
int total; float average; public: void show()
{ total = m1 + m2 + m3; average = (total) /
3; cout << "Total=" << total << endl; cout <<
"average=" << average << endl;
} }; int
main(void) {
result r1;
r1.getstu();
r1.getmarks();
r1.putstu();
r1.putmarks();
r1.show();
return 0;
}
OUTPUT
PROGRAM NO
-18(a)
AIM - Unary operator overloading using member function.
#include <iostream> using
namespace std; class
complex{ private: int a;
int b; public: void
setData(int x,int y){ a=x;
b=y; } void showData(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
} complex operator-
(){ complex temp;
temp.a=-a; temp.b=-
b; return temp;}};
int main(){ complex
c1,c2;
c1.setData(99,23);
c2=-c1;
c2.showData();
return 0;}
OUTPUT
PROGRAM NO
-18(b)
AIM - Unary operator overloading using friend function.
#include <iostream> using
namespace std; class complex
{ private: int a; int b;
public: void setData(int x,
int y) { a = x; b = y; }
void showData() { cout <<
"a=" << a << endl; cout <<
"b=" << b << endl;
}
friend complex operator-(complex);};
complex operator-(complex X) {
complex temp; temp.a = -X.a; temp.b
= -X.b; return temp;} int main(void)
{ complex c1, c2; c1.setData(88,
27); c2 = -c1; c2.showData(); return
0;}
OUTPUT
PROGRAM NO
-19(a)
AIM - Binary operator overloading using member function.
#include <iostream> using
namespace std; class complex {
private: int a; int b; public:
void setData(int x, int y) { a
= x; b = y; } void showData()
{ cout << "a=" << a << endl;
cout << "b=" << b << endl;}
complex operator+(complex c) {
complex temp; temp.a = a +
c.a; temp.b = b + c.b; return
temp;}}; int main(void) {
complex c1, c2, c3;
c1.setData(23, 45);
c2.setData(27, 33); c3 = c1 +
c2; c3.showData(); return 0;}
OUTPUT
PROGRAM NO-19(b)
AIM - Binary operator overloading using friend function.
#include <iostream> using
namespace std; class complex
{ private: int a; int b;
public: void setData(int x,
int y) { a = x; b = y;} void
showData() { cout << "a=" <<
a << endl; cout << "b=" << b
<< endl;}
friend complex operator+(complex X, complex Y);};
complex operator+(complex X, complex Y) {
complex temp; temp.a = X.a + Y.a; temp.b = X.b +
Y.b; return temp;} int main(void){ complex c1,
c2, c3; c1.setData(33, 433); c2.setData(28, 52);
c3 = c1 + c2; c3.showData(); return 0;
}
OUTPUT
PROGRAM NO-20
AIM - Program to illustrate virtual function.
#include <iostream>
using namespace std;
class Base { public:
void display() { cout <<
"Display base" << endl;} virtual
void show() { cout << "Show
Base" << endl;}}; class Derived
: public Base { public:
void display() { cout << "Display
Derived" << endl;} void show() {
cout << "Show Derived" << endl;}};
int main(void) { Base B;
Derived D; Base
*bptr;
cout << "bptr points to base" << endl;
bptr = &B; bptr->display(); bptr-
>show();
cout << "bptr points to Derived" << endl;
bptr = &D; bptr->display(); bptr->show();
return 0;}
OUTPUT