Task 3:
Consider a class Computer having Two fields (i.e. companyName, price) and A single function named show() A class named Desktop inherits Computer class and adds fields representing color, monitor size, and processor type and Override function named show() to display values of its all attributes A class named Laptop inherits Computer class and adds fields representing color, size, weight, and processor type and Override function named show() to display values of its all attributes In Main() instantiate objects of derived classes to access respective show() functions to see the polymorphic behavior.
#include <cstring>
#include <iostream>
using namespace std;
class Computer{
protected:
string companyName = "Unknown";
float price = 0;
public:
virtual void show(){
cout<<"Company name: "<<companyName<<endl;
cout<<"Price: "<<price<<endl;
}
};
class Desktop: public Computer{
protected:
string color = "Unknown";
float monitorSize = 0.0;
string processorType = "Unknown";
public:
void show(){
cout<<"Company name: "<<companyName<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Color: "<<color<<endl;
cout<<"Monitor size: "<<monitorSize<<endl;
cout<<"Processor type: "<<processorType<<endl;
}
};
class Laptop:public Computer{
protected:
string color = "Unknown";
float size = 0;
float weight = 0;
string processorType = "Unknown";
public:
void show(){
cout<<"Company name: "<<companyName<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Color: "<<color<<endl;
cout<<"Size: "<<size<<endl;
cout<<"Weight: "<<weight<<endl;
cout<<"Processor type: "<<processorType<<endl;
}
};
int main(){
Desktop test1;
test1.show();
cout<<endl;
Laptop test2;
test2.show();
cout<<endl;
Computer test3;
test3.show();
return 0;
}
Comments
Leave a comment