Answer to Question #344578 in C++ for khan

Question #344578

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.


1
Expert's answer
2022-05-25T10:23:01-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS