CS201 Assignment 3 Solution Spring 2018
#1

CS201 Assignment 3
Spring 2018

Assignment:

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.


Solution:


Code:
#include <iostream>
#include <string>
using namespace std;
class Laptop
{
   private:
       string Brand, Processor;
       int Ram, hardDrive;
   
   public:
   Laptop()
       {
           cout<<"Default Constructor Called \n";
           Brand = "None";
           Processor = "None";
           Ram = 0;
           hardDrive = 0;
       }
   
       void setBrand(string b)
       { Brand = b; }
       void setProcessor(string p)
       { Processor = p; }
       void setRam(int r)
       { Ram = r; }
       void sethD(int hd)
       { hardDrive = hd; }
   
       string getBrand()
       { return Brand; }
       string getProcessor()
       { return Processor; }
       int getRam()
       { return Ram; }
       int gethD()
       { return hardDrive; }
   
   Laptop(string theBrand, string theProcessor, int theRam, int thehardDrive)
       {
         cout<<"Parameterized Constructor Called...\n";
         Brand = theBrand;
         Processor = theProcessor;
         Ram =  theRam;
         hardDrive = thehardDrive;
       }
   };
int main()
{
 
 Laptop l;
 cout<<"\nBrand : "<<l.getBrand()<<"\nProcessor : "<<l.getProcessor()<<"\nRam : "<<l.getRam()<<"\nHard Drive : "<<l.gethD()<<"\n\n";


 Laptop l2("dell", "i5", 400, 500);
 cout<<"\nBrand : "<<l2.getBrand()<<"\nProcessor : "<<l2.getProcessor()<<"\nRam : "<<l2.getRam()<<"\nHard Drive : "<<l2.gethD()<<"\n";
 system("pause");
}



Save the code in DevC++ and Press F11 to compile and run.
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by Mishi Khan
05-07-2019, 08:18 PM
Last Post by Mishi Khan
02-02-2019, 08:25 PM
Last Post by Mishi Khan
12-22-2018, 07:32 PM



Users browsing this thread: 1 Guest(s)