0% found this document useful (0 votes)
1 views

Waleed Assignment

The document contains three tasks related to C++ programming, focusing on class inheritance and object-oriented principles. Task #1 and Task #2 define a 'vehicle' class and a 'truck' subclass with setter and getter methods for vehicle attributes. Task #3 expands on this by introducing a 'car' class and a 'sportsCar' subclass, implementing constructors and overriding a print_details method to display vehicle information.

Uploaded by

oliviajahraus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Waleed Assignment

The document contains three tasks related to C++ programming, focusing on class inheritance and object-oriented principles. Task #1 and Task #2 define a 'vehicle' class and a 'truck' subclass with setter and getter methods for vehicle attributes. Task #3 expands on this by introducing a 'car' class and a 'sportsCar' subclass, implementing constructors and overriding a print_details method to display vehicle information.

Uploaded by

oliviajahraus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

Task # 1:

#include <iostream>

using namespace std;

class vehicle {

protected:

string manifacturer_name;

int no_of_cylinders;

};

class truck : public vehicle {

float load_capacity;

int towing_capacity;

public:

void setter() {

cout << "Enter manifacturer name: ";

cin >> manifacturer_name;

cout << endl << "Enter no of cylinders: ";

cin >> no_of_cylinders;

cout << endl << "Enter loading capacity of truck in tons: ";

cin >> load_capacity;

cout << endl << "Enter towing capacity of truck in pounds: ";

cin >> towing_capacity;

cout << endl;

void getter() {
cout << "Manifacturer name: " << manifacturer_name << endl;

cout << "No of cylinders: " << no_of_cylinders << endl;

cout << "Loading capacity of truck: " << load_capacity << " tons" << endl;

cout << "Towing capacity of truck: " << towing_capacity << " pounds" << endl;

};

int main() {

truck obj1, obj2;

cout << "Set obj1: " << endl;

obj1.setter();

cout << "Set obj2: " << endl;

obj2.setter();

cout << endl;

cout << "Get obj1: " << endl;

obj1.getter();

cout << "Get obj2: " << endl;

obj2.getter();

Task no 2:
#include <iostream>

using namespace std;

class vehicle {

protected:

string manufacturer_name;

int no_of_cylinders;
};

class truck : public vehicle {

float load_capacity;

int towing_capacity;

public:

void setter() {

cout << "Enter manifacturer name: ";

cin >> manifacturer_name;

cout << endl << "Enter no of cylinders: ";

cin >> no_of_cylinders;

cout << endl << "Enter loading capacity of truck in tons: ";

cin >> load_capacity;

cout << endl << "Enter towing capacity of truck in pounds: ";

cin >> towing_capacity;

cout << endl;

void getter() {

cout << "Manufacturer name: " << manifacturer_name << endl;

cout << "No of cylinders: " << no_of_cylinders << endl;

cout << "Loading capacity of truck: " << load_capacity << " tons" << endl;

cout << "Towing capacity of truck: " << towing_capacity << " pounds" << endl;

};

int main() {

truck obj1, obj2;


cout << "Set obj1: " << endl;

obj1.setter();

cout << "Set obj2: " << endl;

obj2.setter();

cout << endl;

cout << "Get obj1: " << endl;

obj1.getter();

cout << "Get obj2: " << endl;

obj2.getter();

// Task # 3

// #include<iostream>

// using namespace std;

// class vehicle{

// protected:

// string manifacturer_name;

// int no_of_cylinders;

// public:

// // A constructor that takes the name and number of cylinders as parameters

// vehicle(string name, int cylinders){

// manifacturer_name = name;

// no_of_cylinders = cylinders;

// }

// // A virtual function that prints the details of the vehicle

// virtual void print_details(){


// cout << "Manifacturer name: "<< manifacturer_name << endl;

// cout << "No of cylinders: " << no_of_cylinders << endl;

// }

// };

// class truck : public vehicle{

// protected:

// float load_capacity;

// int towing_capacity;

// public:

// // A constructor that takes the name, number of cylinders, load capacity and towing capacity as
parameters

// truck(string name, int cylinders, float load, int tow) : vehicle(name, cylinders){

// load_capacity = load;

// towing_capacity = tow;

// }

// // A function that overrides the print_details function of the vehicle class

// void print_details(){

// // Call the base class function first

// vehicle::print_details();

// cout << "Loading capacity of truck: " << load_capacity <<

" tons" << endl;

// cout << "Towing capacity of truck: " << towing_capacity <<

" pounds" << endl;

// }

// };

// class car : public vehicle{


// protected:

// int model_number ;

// string model_release;

// public:

// // A constructor that takes the name, number of cylinders, model number and model release as
parameters

// car(string name, int cylinders, int model, string release) : vehicle(name, cylinders){

// model_number = model;

// model_release = release;

// }

// // A virtual function that prints the details of the car

// virtual void print_details(){

// // Call the base class function first

// vehicle::print_details();

// cout << "Model number: " << model_number << endl;

// cout << "Model release: " << model_release << endl;

//}

//}

class sports_Car : public car{

protected:

string car_name;

string engine_name;

public:

// A constructor that takes the name, number of cylinders, model number, model release, car name and
engine name as parameters

sports_Car(string name, int cylinders, int model, string release, string carname, string enginename) :
car(name, cylinders, model, release){
car_name = carname;

engine_name = enginename;

// A function that overrides the print_details function of the car class

void print_details(){

// Call the base class function first

car::print_details();

cout << "Car name: " << car_name << endl;

cout << "Engine name: " << engine_name << endl;

};

int main(){

sports_Car obj1("Ferrari", 8, 488, "2019", "Spider", "V8");

sports_Car obj2("Lamborghini", 10, 610, "2020", "Huracan", "V10");

cout << "Get obj1: " << endl;

obj1.print_details();

cout << "Get obj2: " << endl;

obj2.print_details();

You might also like