You are on page 1of 6

Q1. Consider a class named `Car` with private, protected, and public members.

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

// Constructor to initialize speeds


Car(int privateSpeed, int protectedSpeed, int publicSpeed) {
this->privateSpeed = privateSpeed;
this->protectedSpeed = protectedSpeed;
this->publicSpeed = publicSpeed;
}

// Method to display speeds


void displaySpeeds() {
std::cout << "Private speed: " << privateSpeed << std::endl;
std::cout << "Protected speed: " << protectedSpeed << std::endl;
std::cout << "Public speed: " << publicSpeed << std::endl;
}
};

int main() {
Car myCar(100, 120, 140); // Creating an object of the Car class

// Accessing public member


std::cout << "Accessing public speed: " << myCar.publicSpeed <<
std::endl;

myCar.displaySpeeds(); // Demonstrating accessing all speeds through


a member function

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) {}

// Method to calculate area


double calculateArea() {
return length * width;
}

// Method to calculate perimeter


double calculatePerimeter() {
return 2 * (length + width);
}
};

int main() {
// Create a rectangle object with length 5 and width 3
Rectangle rect(5, 3);

// Calculate and display area


std::cout << "Area: " << rect.calculateArea() << std::endl;

// Calculate and display perimeter


std::cout << "Perimeter: " << rect.calculatePerimeter() << std::endl;

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;
}
};

class Car : public Vehicle {


private:
std::string brand;

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;
}
};

class Bike : public Vehicle {


private:
std::string type;

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>

using namespace std;

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;

// Adding items to inventory


manager.addItem("Item1", 10);
manager.addItem("Item2", 5);

// 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>

using namespace std;

// Class to represent a student


class Student {
public:
string name;
int marks;
};

vector<Student> students; // Vector to store student objects

// Function to add a new student


void addStudent() {
Student newStudent;
cout << "Enter student name: ";
cin >> newStudent.name;
cout << "Enter marks: ";
cin >> newStudent.marks;
students.push_back(newStudent);
}

// Function to display all students


void displayStudents() {
cout << "Student Information:" << endl;
for (const auto &student : students) {
cout << "Name: " << student.name << ", Marks: " << student.marks
<< endl;
}
}

// Function to calculate the average marks of all students


double calculateAverageMarks() {
if (students.empty()) {
return 0.0;
}

int totalMarks = 0;
for (const auto &student : students) {
totalMarks += student.marks;
}

return static_cast<double>(totalMarks) / students.size();


}

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;
}

You might also like