0% found this document useful (0 votes)
22 views16 pages

OOP Pract. 15

Uploaded by

Mochi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views16 pages

OOP Pract. 15

Uploaded by

Mochi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

o INDEX *0e*o

Name. Joshi Shweta Kiran

Roll no. 21420

Enrollment no. 2012280095

Subject. OOP
PAGE NO:
DATE

NameSbweta Joshi
Enrolloo 2ol222ce 93s
Gublect0oP

Prccti.cul No 1S

o Yorcqram to TmpJement Paioter Derived cJass

Minimum theoreticd backgound i

LCL4_allowsbase cass pointers o poiot


clerived class obJects
2 Let we bave

class base 2
class derved:publia base u 3
he s we CasLwrite

base*pi
_derived
d-obipi=
&d cb
base*p2 ne derived

3Usingabase class paioder Cpaintinq fa


aderived claS.sobied) wee Can CCCegs
only those embers of he derived
obiecthd wbexe inberitel frem
he bcLSe
PAGE NO

DATE:

a T5 differen om the bebavio


thee Java Sboas
b e can qet Tava ke be havlor uSin9-
vitual fundons

This is because the hase p.aier bas


knoualedge caly the base class.

S knaws mothinq_abad Hbe aersbers


added b4 the derived class

Practi.ca related qyestionsi


A peinde to the base class cun bold address
of
base claS obled as well as_deaived class
obiec

2 bidh varidble stoxes theemarsaddaess ot


Cnather yearidhle
Poioter

Concdusion
T t h i s practicl cue learoedabeu

p r o g a m and theors af pointer


derived class
Coding C++ RUN MENU
Auto saved at 19:30:58

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

12classRectangle: public Polygon


13 public:
14 int area()
15 return width*height; }
16
17

18 Class Triangle: public Polygon


19 public:
20 int area()
return width*height/2; }
22};
23
24int main () {
25 Rectangle rect;
26 Triangle
Polygon *
trgl;
ppoly1 = &rect
27

28 Polygon* ppoly2 =&trgl;


29 Ppoly1->set_values (4,5);
30 Ppoly2->set_values (4,5)
31 cout << rect.area() << ' \ n ' ;
32 Cout << trgl. area() << ' \ n ' ;
33 return 0;
34

Tab

&
Compile Result

20
10

[Process completed - press Enter]


Coding C++ RUN MENU
Auto saved at 19:32:38

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

13class Triangle : public Shape


144
15 public: void display_area ()
16

17 cout"Area of triangle "<<0.5*a*bscendl;


18

19
20

21 class Rectangle : public Shape


22
23 public: void display_area ()
24

25 cout&<"Area of rectangle "e<a*b<sendl;


26

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

Enter base and altitude : 10


7
Area of triangle 35
Enter length and breadth:

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>

2 using namespace std;


3

4 class Shape {
public: double a, b;
6 void get_data ()

8 cin>>a>>b;
9

10 virtual void display_area (O = 0;


11
12

13class Triangle : public Shape


144
15 public: void display_area ()
16

17 cout"Area of triangle "<c0.5*a*b«cendl;


18
19

20

21class Rectangle: public Shape


22 void
25
public: display_area ()
24

25 cout<"Area of rectangle "<<a*b<<endl;


26

27
28
29int main()
30
31 Triangle t;
32 Shape *st = &t;

33 cout<"Enter base and altitude :


34 st->get_data();
35
36
st->display_area()
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

Enter base and altitude : 24


31
Area of triangle 372
Enter length and breadth:

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

Try Try Dcoder's keyboard


15.3.cpp
Saved
16 class DerivedClass :public BaseClass
17
18 int y
19 public:
20 void sety(int i)
21
22 y i;
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 p-&baseObj ect ;
37 P->setx(10);
38 cout << "Base object x: P->getx () «< \n' ;
39
40 p-&derivedobject ; p->setx(99);
41
42 derivedobject.sety (88): <p->getx() <« \
43 cout << "Derived object x:

44
45 cout << "Derived object y: "ec derivedobject.get
46
47
8 return 0; }
49
50

T r y Dcoder's keyboard
x Terminal

Base object x:10


Derived object x: 99
Derived object y: 88

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

E Try Dcoder's keyboard


Terminal

Pointer of base class pointes to it


n1=44

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

Try Try Dcoder's keyboard


X Terminal

Enter width: 4
Enter height: 5
Enter width: 4
Enter height: 5
Area of Rectangle: 20

Area of triangle: 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy