OOP Pract. 15
OOP Pract. 15
Subject. OOP
PAGE NO:
DATE
NameSbweta Joshi
Enrolloo 2ol222ce 93s
Gublect0oP
Prccti.cul No 1S
class base 2
class derved:publia base u 3
he s we CasLwrite
base*pi
_derived
d-obipi=
&d cb
base*p2 ne derived
DATE:
Concdusion
T t h i s practicl cue learoedabeu
1#include <iostream>
2 using namespace std;
4 Class Polygon {
5 protected:
6 int width, height;
7 public:
8 void set_values (int a, int b)
{widthFa; height=b; }
10}
11
Tab
&
Compile Result
20
10
1#include <iostream>
2 Using namespace std;
4 Class Shape {
public: double a, b;
void get_data ()
8
9
cin>>ap>b
10 virtual void display_area () = 0;
11
12
19
20
27
28
29int main()
30{
31 Triangle t;
32 Shape *st = &t;
33 cout<<"Enter base and altitude: ";
34
st->get_data();
35
st->display_area(O:
36
37 Rectangle r;
38 Shape *sr = &r;
39 cout<"Enter length and breadth:
40 sr->get_data();
41
sr->display_area();
42 return 0;
43
44
Tab
=
&
Compile Result
2 3 6 7 8 9 0
q p
asd h K
zx b m
12
?823 EN MR
Coding C++ RUN MENU
Auto saved at 19:32:46
1#include <iostream>
4 class Shape {
public: double a, b;
6 void get_data ()
8 cin>>a>>b;
9
20
27
28
29int main()
30
31 Triangle t;
32 Shape *st = &t;
44
Tab
&
Compile Result
12 3 B6 7 8 90
6 w
q t
u p
as d
zx C V b n m
EN MR
15.3.cpp
Saved
#include <iostream>
2 using name space std;
3 class BaseClass
4
int X;
public:
void setx(int i)
9 X = i;
10
11 int getx()
12
13 return X;
14
15
16 class DerivedClass :public BaseClass
17
18 int y
19 public:
20 void sety(int 1)
21 y = 1;
22
23
24 int gety()
25
26 return y:
27
28
29 int main ()
30
31
32 BaseClass *p; BaseClass
33 base0bject; DerivedClass
34 derivedobject ;
35
36 n=&hase0hiect
44
45 cout << "Derived object y: "ec derivedobject.get
46
47
8 return 0; }
49
50
T r y Dcoder's keyboard
x Terminal
Process finished .
15.2.cpp
Saved
1 #include <iostream>
2 using namespace std;
3 class base
4 {
5 public:
6 int n1;
7 void show()
8
9 coute<"\nn1="<<n1;
10
11
12 class derive :public base
13 {
14 public:
15 int n2;
16 void show ()
17 {
18 cout<e"\nn1="<<n1;
19 coutee"\nn2="<<n2;
20
21
22 int main ()
23 {
24 base b;
25 base *bptr;
26 coutee"Pointer of base class pointes to it";
27 bptr = &b;
28 bptr -> n1 = 44;
29 bptr -> show():
30 derive d;
31 cout<e"n";
32 bptr = &d;
33 bptr->n1 = 66;
34
bptr->show ():
35 return ;
36
n1-66
Process finished.
pointer to derived.cppp
Saved
#include <iostream>
2 using namespace std;
3 class Polygon
4
protected :
intwidth, height ;
public:
void set_values (int a, int b)
9
10 width a;
11 height = b;
12
13 cout<<" \nEnter width : <<a;
14 coutse"\nEnter height: "<<b;
15
16
17 class rectangle :public Polygon
18
19 public:
20 int area()
21
22 return width*height;
23
24
25 class Triangle:public Polygon
26
27 public:
28 int area()
29
30 return width*height/2;
31
32
33 int main ()
34 {
35
36 rectanale rect
Enter width: 4
Enter height: 5
Enter width: 4
Enter height: 5
Area of Rectangle: 20
Area of triangle: 10