Lecture 26 to 29 of cs201... class is explained here...

must watch...... https://www.youtube.com/watch?v=fhi6AidX5yA



Scenario....

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.



NOTE:

You must use setter functions inside the parameterized constructor for initialization of the data members with the values passed by arguments. Also you have to use getter functions in the main() to show the values of data members. Marks will be deducted if any of these requirements is not fulfilled.



Solution code......

#include <iostream>
#include <string.h>
using namespace std;
class Laptop{
private:
        //Data Members
char brand[20], processor[20];
int ram;
int hardDrive;
public:
//Default Constructor of the class
Laptop();   
//parameterized Constructor of the class
Laptop(char[], char[], int, int);
//setter methods
char  setBrand(char[]);
char  setCpu(char[]);
int   setRam(int);
int setHD(int); //Getter Function
char* getBrand();
char* getCpu();
int   getRam();
int   getHD();
};
//Explicit default constructor
Laptop::Laptop()
{
cout<<"Default constructor called...\n\n";
strcpy(brand, "None");
strcpy(processor, "None");
ram = 0;
hardDrive  = 0;
}
//Explicit Parametarized constructor
Laptop::Laptop(char Brand[], char Cpu[], int Ram, int HD)
{
cout<<"Parameterized constructor called...\n\n";
strcpy(brand, Brand);
strcpy(processor, Cpu);
ram = Ram;
hardDrive = HD;
}
//Setter Methods of Class
char Laptop::setBrand(char Brand[])
{
strcpy (brand, Brand);
}
char Laptop::setCpu(char Cpu[])
{
strcpy (processor, Cpu);
}
int Laptop::setRam(int Ram)
{
ram = Ram; }
int Laptop::setHD(int HD)
{
hardDrive = HD;
}
//Getter Methods of the class
char* Laptop::getBrand()
{
    return brand;
} char* Laptop::getCpu()
{
    return processor;
} int Laptop::getRam()
{
    return ram; } int Laptop::getHD()
{
    return hardDrive;
}
main()
{
Laptop L53;
  cout<<"\nBrand : "<<L53.getBrand();
  cout<<"\nProcessor : "<<L53.getCpu();
  cout<<"\nRam : "<<L53.getRam();
  cout<<"\nHard Drive : "<<L53.getHD()<<"\n\n";


  Laptop L2("dell", "i5", 4, 500);
  cout<<"\nBrand : "<<L2.getBrand();
  cout<<"\nProcessor : "<<L2.getCpu();
  cout<<"\nRam : "<<L2.getRam();
  cout<<"\nHard Drive : "<<L2.getHD()<<"\n";

  system("pause");
}
Please like, Subscribe and Share...



See Also....

1. Facebook Page For VU Study.... https://www.facebook.com/vuvickybhai/



2. Facebook Group For assignments, quiz and GDB help..... https://www.facebook.com/groups/vuvicky/



3. Website For Past Papers, Handouts and other material.... http://vuvicky.blogspot.com/



4. Whatsapp Study Group.... https://chat.whatsapp.com/H8xoaZZjYRTBzdiQY5aW0y



5. Whatsapp Group For Only Cs201.... https://chat.whatsapp.com/BmDowutbliN32uuuz786jO



6. Whatsapp Group For Fun.... https://chat.whatsapp.com/3RGoiBvoQcc20uV2PnpWmt



7. Admin's Facebook Page.... https://www.facebook.com/aSc0rpi0/



RELATED:::::

Dev C++ Latest Version.... http://sprysphere.com/6Qgw

2 Comments

Don't Forget To Join My FB Group VU Vicky
THANK YOU :)

Post a Comment

Don't Forget To Join My FB Group VU Vicky
THANK YOU :)

Previous Post Next Post