Problem 1: Rectangle Area Calculator
Scenario: A graphics program needs to calculate areas of multiple rectangles.
Task: Create a Rectangle class:
● Private: length, width
● Constructors:
○ Default constructor (10 × 5)
○ Parameterized constructor
● Function area()
#include <iostream>
using namespace std;
class Rectangle {
private:
int length, width;
public:
Rectangle() {
length = 10;
width = 5;
}
Rectangle(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
};
int main() {
Rectangle r1;
Rectangle r2(8, 6);
cout << "Area r1: " << [Link]() << endl;
cout << "Area r2: " << [Link]() << endl;
return 0;
}
Problem 2: Student Record System
Scenario A university wants to store student details and calculate whether a student
has passed.
Task: Create a Student class with:
● Private data members: rollNo, name, marks
● Public member functions:
○ setData() to initialize values
○ isPassed() → returns true if marks ≥ 40
○ display() to print student details
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
string name;
float marks;
public:
void setData(int r, string n, float m) {
rollNo = r;
name = n;
marks = m;
}
bool isPassed() {
return marks >= 40;
}
void display() {
cout << "Roll No: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Marks: " << marks << endl;
cout << (isPassed() ? "Status: Passed" : "Status: Failed")
<< endl;
}
};
int main() {
Student s1;
[Link](101, "Alice", 72.5);
[Link]();
return 0;
}
Problem 3: Bank Account Management
Scenario: A bank wants to manage customer accounts with deposit and withdrawal
features.
Task: Create an Account class with:
● Private: accountNo, balance
● Public:
○ Constructor to initialize account number and balance
○ deposit(amount)
○ withdraw(amount)
○ display()
#include <iostream>
using namespace std;
class Account {
private:
int accountNo;
double balance;
public:
Account(int accNo, double bal) {
accountNo = accNo;
balance = bal;
void deposit(double amount) { balance += amount;}
void withdraw(double amount) {
if (amount <= balance)
balance -= amount;
else
cout << "Insufficient balance!" << endl;
}
void display() {
cout << "Account No: " << accountNo << endl;
cout << "Balance: " << balance << endl;
}
};
int main() {
Account a1(1001, 5000);
[Link](1500);
[Link](2000);
[Link]();
return 0;
Constructor
Problem 1: Default & Parameterized Constructor (Employee System)
Scenario
A company wants to store employee salary details. If no data is given, a default salary
should be assigned.
Task: Create a class Employee with:
● Private: id, salary
● Default constructor → sets id = 0, salary = 10000
● Parameterized constructor → initializes values
● Function display()
#include <iostream>
using namespace std;
class Employee {
private:
int id;
double salary;
public:
Employee() { // Default constructor
id = 0;
salary = 10000;
}
Employee(int i, double s) { // Parameterized constructor
id = i;
salary = s;
}
void display() {
cout << "ID: " << id << " Salary: " << salary << endl;
}
};
int main() {
Employee e1;
Employee e2(101, 35000);
[Link]();
[Link]();
return 0;
}
Problem 2: Copy Constructor (Book Store)
Scenario: A bookstore wants to duplicate book objects safely.
Task: Create a Book class:
● Private: price
● Parameterized constructor
● Copy constructor
● Function display()
#include <iostream>
using namespace std;
class Book {
private:
float price;
public:
Book(float p) {
price = p;
}
Book(const Book &b) { // Copy constructor
price = [Link];
}
void display() {
cout << "Price: " << price << endl;
}
};
int main() {
Book b1(499.50);
Book b2 = b1; // Copy constructor called
[Link]();
[Link]();
return 0;
}
Problem 3: Constructor + Dynamic Memory (Student Name)
Scenario: A system stores student names dynamically.
Task: Create a Student class:
● Private: char* name
● Parameterized constructor
● Copy constructor
● Destructor
● Function display()
#include <iostream>
#include <cstring>
using namespace std;
class Student {
private:
char* name;
public:
Student(const char* n) {
name = new char[strlen(n) + 1];
strcpy(name, n);
}
Student(const Student &s) { // Copy constructor
name = new char[strlen([Link]) + 1];
strcpy(name, [Link]);
}
void display() {
cout << "Name: " << name << endl;
}
~Student() {
delete[] name;
}
};
int main() {
Student s1("Alice");
Student s2 = s1;
[Link]();
[Link]();
return 0;
}
Problem 4: Rectangle Class Using .h and .cpp Files
Scenario: A program calculates rectangle area using multiple files.
rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle {
private:
int length, width;
public:
Rectangle(int l, int w);
int area();
};
#endif
[Link]
#include "rectangle.h"
Rectangle::Rectangle(int l, int w) {
length = l;
width = w;
}
int Rectangle::area() {
return length * width;
}
[Link]
#include <iostream>
#include "rectangle.h"
using namespace std;
int main() {
Rectangle r(5, 8);
cout << "Area: " << [Link]() << endl;
return 0;
}
How to combine these files and run????