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.
  


Possibly Related Threads…
Thread Author Replies Views Last Post
  CS201 Assignment 1 Solution Fall 2019 Ayra Suleman 0 224 11-11-2019, 01:21 AM
Last Post: Ayra Suleman
  CS205 GDB Solution Spring 2019 Ayra Suleman 0 372 08-10-2019, 01:16 AM
Last Post: Ayra Suleman
  CS202 Assignment 3 Solution Spring 2019 Ayra Suleman 0 218 07-20-2019, 02:39 PM
Last Post: Ayra Suleman
  CS201 Assignment 3 Solution Spring 2019 Ayra Suleman 0 167 07-11-2019, 10:21 PM
Last Post: Ayra Suleman
  CS206 Assignment 2 Solution Spring 2019 Mishi Khan 0 326 05-29-2019, 02:01 AM
Last Post: Mishi Khan
  CS201 Assihgnment Mishi Khan 0 349 05-07-2019, 08:18 PM
Last Post: Mishi Khan
  CS201-MidTerm Past paper Mishi Khan 0 939 12-22-2018, 07:32 PM
Last Post: Mishi Khan
  CS201 Assignment 1 Solution Fall 2018 Admin 2 3,794 12-03-2018, 10:07 PM
Last Post: Admin
  CS201 Assignment 2 Solution Spring 2018 Admin 4 6,937 05-28-2018, 02:36 PM
Last Post: Pakistani
  Assignment Solution CS201 Spring 2018 Syra Younas 3 3,588 05-07-2018, 09:46 PM
Last Post: Admin



Users browsing this thread:
1 Guest(s)