100% found this document useful (1 vote)
626 views7 pages

OOP Practical 15

The document describes an experiment on inheritance and polymorphism in C++. It contains code to define a base Polygon class and derived Rectangle and Triangle classes that inherit from Polygon. The code creates Polygon pointers that point to Rectangle and Triangle objects and calls the area() method, which is polymorphically defined in the derived classes. The output of the program is the calculated areas of the rectangle and triangle.

Uploaded by

Raj Gharat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
626 views7 pages

OOP Practical 15

The document describes an experiment on inheritance and polymorphism in C++. It contains code to define a base Polygon class and derived Rectangle and Triangle classes that inherit from Polygon. The code creates Polygon pointers that point to Rectangle and Triangle objects and calls the area() method, which is polymorphically defined in the derived classes. The output of the program is the calculated areas of the rectangle and triangle.

Uploaded by

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

Subject: object oriented programming using c++ Subject Code:22316

Semester:3 Course: CO31A

Laboratory No: L003A Name of Subject Teacher: Sayali kadam

Name of Student: Vedant Pendharkar Roll Id:20203A00013

Experiment No: 15

Title of Experiment Implement Inheritance in C++ program. Use Polymorphism in C++


program.

Program

// pointers to base class


#include <iostream>
using namespace std;

class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};

class Rectangle: public Polygon {


public:
int area()
{ return width*height; }
};

class Triangle: public Polygon {


public:
int area()
{ return width*height/2; }
};

int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << '\n';
cout << trgl.area() << '\n';
return 0;
}

Output
1. Complete the given table:
Write & justify
Program Code
Output
a) The output of the
#include<iostream.h> following code is
class base error because we
{ should use
public: <iostream>
int nl; Instead of .h>
void show()
{

cout<<"\nnl " <<nl;

} ;

class derive public base

public:
int n2;
void show()
{
cout<<"\nnl "<<nl;
cout<<"\nn2 "<<n2;

} ;

int main()
{
base b;
base *bptr;
cout<<"Pointer of base class points to it";
bptr=&b;
bptr->n1=44;
bptr->show();
derive d;
cout<<"\n";
bptr=&d;
bptr->n1=66;
bptr->show();
return 0;
b) #include <iostream.h> The output of the
class BaseClass {
following code is
int
x; error because
public: baseclass was not
void setx(int i) declared
X = i;

int getx()
return x;

} ;

class DerivedClass public BaseClass {


int y;
public:
void sety(int i)
y = i;

int gety()
return y;
}
};

int main()
{
BaseClass *p; BaseClass
baseObject; DerivedClass
derivedObject;

p = &baseObject;
p->setx (l0);
cout << "Base object x : " << p->getx () <<
I \n I;
p = &derivedObject;
p->setx (99);

derivedObject.sety(88);
cout << "Derived object x : " << p->getx () <<
I \n I;
cout << "Derived object y: " <<
derivedObject.gety() << '\n';

return 0;
Practical Related Questions

1) State the output of the following code

#include<iostream.h> class base {


public:
void show()
cout << "base\n";

};
class derived : public base public:
void show() {
cout << "derived\n ";

};
void main() { base bl; bl.show (); derived dl; dl.show (); base *pb = &bl; pb-
>show ();
pb = &dl; pb->show();
The output of the code is Error

2) A pointer to the base class can hold address of


ANS: Only derived class objects.

3) Which variable stores the memory address of another variable?


ANS:Pointer

You might also like