OOP Practical 15
OOP Practical 15
Experiment No: 15
Program
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
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()
{
} ;
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;
} ;
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
};
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