How to Write a C++ program that will create a class named Laptop.

Vu Old Solved Programming Based Assignments Codes are Available in this Section.
Post Reply
User avatar
ALI RAZA
★★★★★★★★★★★★★
★★★★★★★★★★★★★
Posts: 177
Joined: Fri Dec 22, 2017 8:10 am
Location: Lahore
Has thanked: 6 times
Been thanked: 7 times
Contact:
Status: Offline

How to Write a C++ program that will create a class named Laptop.

Post by ALI RAZA » Sat Sep 29, 2018 9:49 am

Write a C++ program that will create a class named Laptop.

This class will have 4 data members
1. brand
2. processor
3. ram
4. hardDrive

Also write getter and setter functions for each data member of the class. For example:
void setbrand(char[]);
void setCpu(char[]);
…………….etc

int getRam();
int getHD();

………………… etc

You are required to write a default and a parameterized constructor for this class.

In the default constructor, you will initialize all the data members with default values. The message “Default constructor called…” should be displayed whenever an object is created using default constructor.

In parameterized constructor, you will initialize all the data members with the values passed to it as arguments. You are required to use setter functions inside parameterized constructor to initialize the data members. The message “Parameterized constructor called…” should be displayed whenever an object is created with parameterized constructor.

In main() function, create 2 objects (1 using default and 1 using parameterized constructor) and display the values of data members of both objects using getter functions.


Answer:

Code: Select all

#include <iostream>
#include <string.h>
using namespace std;

class Laptop{
	
	private:
		char brand[15];
		char cpu[10];
		int ram;
		int hardDrive;
		
	
	public:
		Laptop();
		Laptop(char[], char[], int, int);
		
		
		void setbrand(char[]);
		void setCpu(char[]);
		void setRam(int);
		void setHD(int);
		
		char* getbrand();
		char* getCpu();
		int getRam();
		int getHD();
		
		
};

Laptop::Laptop()
{
	cout << endl<< "Default constructor called..." << endl;
	strcpy(cpu, "None");
	strcpy(brand,"None");
	ram = 0;
	hardDrive = 0;
}

Laptop::Laptop(char Brand[], char CPU[], int RAM, int HD)
 {
 	cout << "Parameterized constructor called..." << endl;

 	
 	this->setbrand(Brand);
 	this->setCpu(CPU);
 	this->setRam(RAM);
 	this->setHD(HD);
 	
 }

void Laptop::setCpu(char CPU[])
 {
 	strcpy(cpu, CPU);
 	
 }

void Laptop::setbrand(char Brand[])
 {
 	strcpy(brand, Brand);
 	
 }


void Laptop::setRam(int RAM)
 {
 	ram = RAM;
 }

void Laptop::setHD(int HD)
 {
 	hardDrive = HD;
 }
 
 
 
 
 char* Laptop :: getbrand()
  {
  	return brand;
  }
 
 char* Laptop :: getCpu()
  {
  	return cpu;
  }
 
 int Laptop::getRam()
  {
  	return ram;
  }
 
 int Laptop::getHD()
  {
  	return hardDrive;
  }





main ()
{
Laptop C1;
cout << endl << "Brand : " << C1.getbrand();
cout << endl << "Processor : " << C1.getCpu();
cout << endl << "RAM : " << C1.getRam();
cout << endl << "Hard Drive : " << C1.getHD();
cout << endl << endl;

Laptop C2("dell", "i5", 4, 500);
cout << endl << "Brand : " << C2.getbrand();
cout << endl << "Processor : " << C2.getCpu();
cout << endl << "RAM : " << C2.getRam();
cout << endl << "Hard Drive : " << C2.getHD();
cout << endl << endl;



}



Post Reply

Last 10 Members Who Visited This Topic

  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest