Answer to Question #261105 in C++ for Zeeshan

Question #261105

Write a base class Computer that contains data members of wordsize(in bits),



memorysize (in megabytes), storagesize (in megabytes) and speed (in megahertz).



Derive a Laptop class that is a kind of computer but also specifies the object’s length,



width, height, and weight. Member functions for both classes should include a default



constructor, a constructor to inialize all components and a function to display data



members.

1
Expert's answer
2021-11-04T11:43:17-0400
#include <iostream>
#include <string>
using namespace std;




class Computer{
private:
	int wordsize;
	float memorysize;
	float storagesize;
	float speed;
public:
	Computer(){}


	Computer(int wordsize,float memorysize,float storagesize,float speed){
		this->wordsize=wordsize;
		this->memorysize=memorysize;
		this->storagesize=storagesize;
		this->speed=speed;
	}


	void display(){
		cout<<"Word size: "<<wordsize<<" bits\n";
		cout<<"Memory size: "<<memorysize<<" megabytes\n";
		cout<<"Storage size: "<<storagesize<<" megabytes\n";
		cout<<"Speed: "<<speed<<" megahertz\n";
	}
};


class Laptop:public Computer{
private:
	float length;
	float width;
	float weight;
public:
	Laptop(){}


	Laptop(int wordsize,float memorysize,float storagesize,float speed,float length,float width,float weight):
	Computer(wordsize,memorysize,storagesize,speed){
		this->length=length;
		this->width=width;
		this->weight=weight;
	}
	
	void display(){
		Computer::display();
		cout<<"Length: "<<length<<"\n";
		cout<<"Width: "<<width<<"\n";
		cout<<"Weight: "<<weight<<"\n";
	}
};


int main()  {
	Laptop laptop(55,2.6,520,2.5,20,30,40);
	laptop.display();
	
	int t=0;
	cin>>t;
	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
New on Blog
APPROVED BY CLIENTS