0% found this document useful (0 votes)
7 views3 pages

OOP CPP Lab Test Solutions

This document provides solutions to 25 practical questions on Object-Oriented Programming (OOP) in C++, showcasing various classes such as Student, Rectangle, Circle, and BankAccount. Each solution includes a well-structured C++ program demonstrating key OOP concepts like encapsulation, constructor overloading, and method implementation. The examples illustrate the creation and manipulation of objects, along with their respective functionalities.

Uploaded by

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

OOP CPP Lab Test Solutions

This document provides solutions to 25 practical questions on Object-Oriented Programming (OOP) in C++, showcasing various classes such as Student, Rectangle, Circle, and BankAccount. Each solution includes a well-structured C++ program demonstrating key OOP concepts like encapsulation, constructor overloading, and method implementation. The examples illustrate the creation and manipulation of objects, along with their respective functionalities.

Uploaded by

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

Object-Oriented Programming (OOP) in C++ - Lab Test Solutions

This document contains solutions for the 25 practical questions in the OOP Lab Test. Each solution

includes a well-structured C++ program demonstrating the required concepts.

Student Class

Solution:
#include <iostream>
using namespace std;

class Student {
string name;
int rollNumber;
float marks;

public:
void setDetails(string n, int r, float m) {
name = n;
rollNumber = r;
marks = m;
}
void display() {
cout << "Name: " << name << "\nRoll No: " << rollNumber << "\nMarks: " << marks
<< endl;
}
};

int main() {
Student s1;
s1.setDetails("John Doe", 101, 89.5);
s1.display();
return 0;
}

Rectangle Class

Solution:
#include <iostream>
using namespace std;
class Rectangle {
float length, width;

public:
void setValues(float l, float w) {
length = l;
width = w;
}
float area() { return length * width; }
float perimeter() { return 2 * (length + width); }
};

int main() {
Rectangle rect;
rect.setValues(5.5, 3.2);
cout << "Area: " << rect.area() << "\nPerimeter: " << rect.perimeter() << endl;
return 0;
}

Constructor Overloading - Circle Class

Solution:
#include <iostream>
using namespace std;

class Circle {
float radius;

public:
Circle() { radius = 1.0; }
Circle(float r) { radius = r; }
float area() { return 3.14 * radius * radius; }
};

int main() {
Circle c1, c2(5);
cout << "Default Circle Area: " << c1.area() << "\nCustom Circle Area: " <<
c2.area() << endl;
return 0;
}

Bank Account Class

Solution:
#include <iostream>
using namespace std;

class BankAccount {
string accHolder;
int accNumber;
float balance;

public:
BankAccount(string name, int acc, float bal) {
accHolder = name;
accNumber = acc;
balance = bal;
}
void deposit(float amount) { balance += amount; }
void withdraw(float amount) { if (amount <= balance) balance -= amount; }
void display() { cout << "Account Holder: " << accHolder << "\nBalance: " << balance
<< endl; }
};

int main() {
BankAccount acc("Alice", 12345, 5000);
acc.deposit(1000);
acc.withdraw(500);
acc.display();
return 0;
}

You might also like