Coding
Coding
Create an
object of the class and demonstrate how each type of access specifier works in accessing the
members.
#include <iostream>
class Car {
private:
int privateSpeed; // Private member
protected:
int protectedSpeed; // Protected member
public:
int publicSpeed; // Public member
int main() {
Car myCar(100, 120, 140); // Creating an object of the Car class
return 0;
}
Q2. Create a class `Rectangle` with private attributes length and width. Implement methods
inside the class to calculate the area and perimeter of the rectangle. Use appropriate access
specifiers.
#include <iostream>
class Rectangle {
private:
double length;
double width;
public:
// Constructor
Rectangle(double l, double w) : length(l), width(w) {}
int main() {
// Create a rectangle object with length 5 and width 3
Rectangle rect(5, 3);
return 0;
}
Q3. Design a base class Vehicle with protected data members like speed and colour. Derive
classes Car and Bike with additional features. Implement methods to display vehicle details.
#include <iostream>
#include <string>
class Vehicle {
protected:
int speed;
std::string color;
public:
Vehicle(int _speed, std::string _color) : speed(_speed),
color(_color) {}
void displayDetails() {
std::cout << "Vehicle Details:" << std::endl;
std::cout << "Speed: " << speed << " km/h" << std::endl;
std::cout << "Color: " << color << std::endl;
}
};
public:
Car(int _speed, std::string _color, std::string _brand)
: Vehicle(_speed, _color), brand(_brand) {}
void displayDetails() {
Vehicle::displayDetails();
std::cout << "Brand: " << brand << std::endl;
}
};
public:
Bike(int _speed, std::string _color, std::string _type)
: Vehicle(_speed, _color), type(_type) {}
void displayDetails() {
Vehicle::displayDetails();
std::cout << "Type: " << type << std::endl;
}
};
int main() {
Car myCar(120, "Red", "Toyota");
Bike myBike(80, "Blue", "Mountain");
myCar.displayDetails();
std::cout << std::endl;
myBike.displayDetails();
return 0;
}
Q4. Design a class for managing inventory items with private data members. Use access
specifiers to control access and implement methods for item addition and display.
#include <iostream>
#include <string>
#include <vector>
class InventoryManager {
private:
struct Item {
string name;
int quantity;
};
vector<Item> inventory;
public:
void addItem(const string& itemName, int quantity) {
Item newItem;
newItem.name = itemName;
newItem.quantity = quantity;
inventory.push_back(newItem);
}
void displayInventory() {
cout << "Inventory:\n";
for (const auto& item : inventory) {
cout << item.name << " - Quantity: " << item.quantity <<
endl;
}
}
};
int main() {
InventoryManager manager;
// Displaying inventory
manager.displayInventory();
return 0;
}
Q5 .Design a simple program to manage student information using methods in C++. The
program should be able to add new students, display student details, and calculate the
average marks of all students. Define some functions outside class using the scope
resolution operator.
#include <iostream>
#include <vector>
int totalMarks = 0;
for (const auto &student : students) {
totalMarks += student.marks;
}
int main() {
int choice;
do {
cout << "\n1. Add new student\n2. Display student details\n3.
Calculate average marks\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayStudents();
break;
case 3:
cout << "Average Marks: " << calculateAverageMarks() <<
endl;
break;
case 4:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
break;
}
} while (choice != 4);
return 0;
}