TASK 1
Given the following classes, write the code for the BBAStudent class so that the
following output is printed when we run the TestStudent class.
Driver code Tester Class
int main()
class Student { { BBAStudent
private: b1;
string name = "Just a Student"; // Default value for name BBAStudent b2("Humty Dumty");
string department = "nothing"; // Default value for department BBAStudent b3("Little Bo Peep");
b1.details();
public: cout << "1---------------" << endl;
// Method to update the department b2.details();
void updateDepartment(string dpt) cout << "2---------------" << endl;
{department = dpt; b3.details();
}
return 0;
// Method to update the name }
void updateName(string name) {
this->name = name; // 'this->' is used to differentiate the
member variable from the local parameter
}
// Method to display student details
void details() {
cout << "Name: " << name << " Department: " <<
department << endl;
}
};
Output:
Name: Just a Student Department: BBA
1
Name: Humty Dumty Department: BBA
2
Name: Little Bo Peep Department: BBA
Solution:
#include <iostream>
#include <string>
using namespace std;
class Student
{private:
string name = "Just a Student"; // Default name for every student
string department = "nothing"; // Default department
public:
void updateDepartment(string dpt) {
department = dpt; // Method to update the department
}
void updateName(string name) {
this->name = name; // Method to update the name
}
void details() {
// Prints the student's details
cout << "Name: " << name << " Department: " << department << endl;
}
};
// BBAStudent class inheriting from Student
class BBAStudent : public Student {
public:
BBAStudent() {
updateDepartment("BBA"); // Set the department to "BBA" for all BBA students
}
BBAStudent(string name) {
updateDepartment("BBA"); // Set the department to "BBA"
updateName(name); // Set the name of the student
}
};
// Tester class
int main() {
BBAStudent b1; // Default constructor is called; name remains "Just a Student"
BBAStudent b2("Humty Dumty"); // Name is set to "Humty Dumty"
BBAStudent b3("Little Bo Peep"); // Name is set to "Little Bo Peep"
b1.details(); // Prints details of b1 (default name and BBA department)
cout << "1--------------- " << endl;
b2.details(); // Prints details of b2 (custom name and BBA department)
cout << "2--------------- " << endl;
b3.details(); // Prints details of b3 (custom name and BBA department)
return 0;
}
TASK 2
Driver code Tester Class
// Tester Class
class Animal { int main() {
public: Dog dog("Buddy", 5, "Brown",
string name; "Bulldog");
int age; Cat cat("Kitty", 3, "White", "Persian");
string color;
cout << "1.========" << endl;
Animal(string name, int age, string color) { cout << dog.info();
this->name = name; cout << "2.========" << endl;
this->age = age; cout << cat.info();
this->color = color; cout << "3.========" << endl;
} dog.makeSound();
cout << "4.========" << endl;
void makeSound() { cat.makeSound();
cout << "Animal makes a sound" << endl;
} return 0;
}
string info() {
return "Name: " + name + "\nAge: " + to_string(age) +
"\nColor: " + color + "\n";
}
};
Output:
1.= = = = = = = =
Name: Buddy
Age: 5
Color: Brown
Breed: Bulldog
2.= = = = = = = =
Name: Kitty
Age: 3
Color: White
Breed: Persian
3.= = = = = = = =
Brown color Buddy is barking
4.= = = = = = = =
White color Kitty is meowing
Solution:
#include <iostream>
#include <string>
using namespace std;
// Derived Dog Class
class Dog : public Animal {
private:
string breed;
public:
Dog(string name, int age, string color, string breed) : Animal(name, age, color), breed(breed) {}
string info() override {
return Animal::info() + "Breed: " + breed + "\n";
}
void makeSound() override {
cout << color << " color " << name << " is barking" << endl;
}
};
// Derived Cat Class
class Cat : public Animal {
private:
string breed;
public:
Cat(string name, int age, string color, string breed) : Animal(name, age, color), breed(breed) {}
string info() override {
return Animal::info() + "Breed: " + breed + "\n";
}
void makeSound() override {
cout << color << " color " << name << " is meowing" << endl;
}
};
Task 3:
Given the following classes, write the code for the Vehicle2010 class to print the following output when we run the
Vehicle2010User class.
Driver Code Output
#include <iostream> (0,0)
using namespace std; (-1,-1)
(0,0)
// Base Vehicle class (1,1)
class Vehicle { (2,0)
public:
int x, y;
Vehicle() : x(0), y(0) {} // Constructor initializing position to (0, 0)
void moveUp() {
y = y + 1;}
void moveDown() {
y = y - 1;}
void moveLeft() {
x = x - 1;}
void moveRight() {
x = x + 1;}
void position() {
cout << "(" << x << "," << y << ")" << endl;}
};
// Tester Class
int main() {
Vehicle2010 car1;
car1.position();
car1.moveLowerLeft();
car1.position();
Vehicle2010 car2;
car2.position();
car2.moveUpperRight();
car2.position();
car2.moveLowerRight();
car2.position();
return 0;
}
Solution:
#include <iostream>
using namespace std;
// Base Vehicle class
class Vehicle {
public:
int x, y;
Vehicle() : x(0), y(0) {} // Constructor initializing position to (0, 0)
void moveUp() {
y = y + 1;
}
void moveDown() {
y = y - 1;
}
void moveLeft() {
x = x - 1;
}
void moveRight() {
x = x + 1;
}
void position() {
cout << "(" << x << "," << y << ")" << endl;
}
};
// Derived Vehicle2010 class
class Vehicle2010 : public Vehicle {
public:
// Move to the lower left: left and down
void moveLowerLeft() {
moveLeft();
moveDown();
}
// Move to the upper right: right and up
void moveUpperRight() {
moveRight();
moveUp();
}
// Move to the lower right: right and down
void moveLowerRight() {
moveRight();
moveDown();
}
};
// Tester Class
int main() {
Vehicle2010 car1;
car1.position();
car1.moveLowerLeft();
car1.position();
Vehicle2010 car2;
car2.position();
car2.moveUpperRight();
car2.position();
car2.moveLowerRight();
car2.position();
return 0;
}