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 2 Solution Spring 2018 Admin 4 3,839 05-28-2018, 02:36 PM
Last Post: Pakistani
  Assignment Solution CS201 Spring 2018 Syra Younas 3 1,808 05-07-2018, 09:46 PM
Last Post: Admin
  Cs201 Syra Younas 2 1,283 05-05-2018, 11:01 AM
Last Post: Muhammad Yaseen
  CS201 GDB Solution 2017 Rubaisha(Moody Girl) 3 1,964 01-10-2018, 10:16 AM
Last Post: fahadmukhtar
  cs201 GBD no 1 solution fahadmukhtar 1 1,527 01-07-2018, 10:12 AM
Last Post: Admin
  CS201 Assignment 2 Solution Fall 2017 Admin 0 1,138 11-22-2017, 09:09 PM
Last Post: Admin
  CS201 VIVA Questions and Answers Pakistani 1 7,324 02-06-2017, 05:26 PM
Last Post: Admin
  CS201 Assignment#2 Solution by Aly Hamza Aly Hamza 0 2,518 06-23-2016, 02:55 PM
Last Post: Aly Hamza
  CS201 Spring 2016 Assignment #1 Solution Aly Hamza 2 2,556 05-11-2016, 10:18 AM
Last Post: Rubaisha(Moody Girl)



Users browsing this thread:
2 Guest(s)