C++ BCA FINAL LAB MANUAL (1)
C++ BCA FINAL LAB MANUAL (1)
230GCAB03_02
Practical (P): 2
2. Accept a score from the user and display the corresponding grade (A, B, C, D, F) using if-
else statements.
3. Print various number patterns (like pyramids or triangles) using nested loops.
4. Create a class Person with attributes like name and age. Create an object of the class and
display the details using a method.
5. Write a C++ program to implement a class called Employee that has private member
variables for name, employee ID, and salary. Include member functions to calculate and
set salary based on employee performance.
9 Implement a class Employee that has private attributes like name, id, and salary. Provide
methods to set and get these attributes, ensuring proper encapsulation.
10 Define a class Circle with attributes radius and methods to calculate the area and
circumference of the circle within the class.(Function within the class)
11 Create a base class Vehicle with attributes like make and model. Derive a class Car that
adds attributes like number_of_seats. Implement a method in the derived class to display
the complete details of the car.
12 Design two base classes Student and Sports with respective attributes. Create a derived
class Result that inherits from both and calculates the total score.
13 Write a program to create a base class Shape with a virtual method calculateArea(). Derive
classes Circle and Rectangle that implement the calculateArea() method differently.
14 Create a class FileManager with methods to write data to a file and read data from the file.
Use objects to manage file operations.
15 Write a program to divide two numbers. Handle the case where the denominator is zero
using a try-catch block.
Program:
#include <iostream>
int main() {
return 0;
Expected Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
2.Accept a score from the user and display the corresponding grade (A, B, C,
D, F) using if-else statements
Program:
#include <iostream>
int main() {
int score;
else if (score >= 80) cout << "Grade: B" << endl;
else if (score >= 70) cout << "Grade: C" << endl;
else if (score >= 60) cout << "Grade: D" << endl;
return 0;
Expected Output:
Grade: B
#include <iostream>
int main() {
int rows = 5;
return 0;
Expected Output:
**
***
****
*****
4. Create a class Person with attributes like name and age. Create an object
of the class and display the details using a method.
Program:
#include <iostream>
#include <string>
public:
string name;
int age;
void display() {
};
int main() {
Person person1;
person1.name = "John";
person1.age = 30;
person1.display();
return 0;
Expected Output:
Name: ABC
Age: 30
Program:
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string name;
int id;
double salary;
public:
name = empName;
id = empID;
salary = empSalary;
void display() {
};
int main() {
Employee emp1;
emp1.display();
return 0;
Expected Output:
Name: Alice
ID: 101
Salary: 5000
Code:
#include <iostream>
int main() {
int intNum = 5;
cout << "Square of " << intNum << " is " << findSquare(intNum) << endl;
cout << "Square of " << doubleNum << " is " << findSquare(doubleNum) <<
endl;
return 0;
Output:
Square of 5 is 25
#include <iostream>
class BankAccount {
public:
double balance;
BankAccount(double initBalance) {
balance = initBalance;
balance += amount;
cout << "Deposited " << amount << ", Balance: " << balance << endl;
balance -= amount;
cout << "Withdrew " << amount << ", Balance: " << balance << endl;
} else {
}
}
};
int main() {
BankAccount account(1000);
account.deposit(200);
account.withdraw(500);
return 0;
Output:
Insufficient funds
#include <iostream>
class Demo {
public:
// Constructor
Demo() {
// Destructor
~Demo() {
};
int main() {
Demo obj;
return 0;
OUTPUT
9. Implement a class Employee that has private attributes like name, id,
and salary. Provide methods to set and get these attributes, ensuring
proper encapsulation.
#include <iostream>
class Employee {
private:
string name;
int id;
double salary;
public:
name = empName;
id = empId;
salary = empSalary;
} else {
salary = 0;
// Getter methods
string getName() {
return name;
int getId() {
return id;
}
double getSalary() {
return salary;
void displayEmployee() {
};
int main() {
Employee emp;
emp.setName("John Doe");
emp.setId(101);
emp.setSalary(50000);
emp.displayEmployee();
return 0;
OUTPUT:
Employee Details:
ID: 101
Salary: $50000
10. Define a class Circle with attributes radius and methods to calculate
the area and circumference of the circle within the class.(Function within
the class)
#include <iostream>
class Circle {
private:
double radius;
public:
Circle(double r) {
radius = r;
double area() {
double circumference() {
void display() {
};
int main() {
Circle c(5);
c.display();
return 0;
OUTPUT:
Radius: 5
Area: 78.5397
Circumference: 31.4159
11. Create a base class Vehicle with attributes like make and model.
Derive a class Car that adds attributes like number_of_seats. Implement a
method in the derived class to display the complete details of the car.
#include <iostream>
// Base class
class Vehicle {
protected:
string make;
string model
public:
make = mk;
model = mdl;
void displayVehicleDetails() {
};
// Derived class
private:
int number_of_seats;
public:
number_of_seats = seats;
void displayCarDetails() {
};
int main() {
myCar.displayCarDetails();
return 0;
OUTPUT:
Car Details:
Make: Toyota
Model: Camry
Number of Seats: 5
12. Design two base classes Student and Sports with respective
attributes. Create a derived class Result that inherits from both and
calculates the total score.
#include <iostream>
class Student {
protected:
string name;
int rollNo;
int marks;
public:
name = n;
rollNo = r;
marks = m;
void displayStudentDetails() {
};
class Sports {
protected:
int sportsScore;
public:
Sports(int s) {
sportsScore = s;
}
void displaySportsScore() {
};
public:
void displayTotalScore() {
cout << "Total Score (Academic + Sports): " << (marks + sportsScore) <<
endl;
};
int main() {
// Displaying details
student1.displayStudentDetails();
student1.displaySportsScore();
student1.displayTotalScore();
return 0;
OUTPUT:
Marks (Academic): 85
Sports Score: 15
#include <iostream>
class Shape {
public:
};
private:
double radius;
public:
Circle(double r) {
radius = r;
};
private:
public:
Rectangle(double l, double w) {
length = l;
width = w;
};
int main() {
shape1->calculateArea();
shape2->calculateArea();
// Free memory
delete shape1;
delete shape2;
return 0;
OUTPUT:
Rectangle Area: 24
14.Create a class FileManager with methods to write data to a file and read
data from the file. Use objects to manage file operations.
#include <iostream>
class FileManager {
private:
public:
FileManager(string fname) {
filename = fname;
if (outFile.is_open()) {
} else {
void readFromFile() {
string line;
if (inFile.is_open()) {
} else {
};
int main() {
FileManager file("data.txt");
file.readFromFile();
return 0;
OUTPUT:
15. Write a program to divide two numbers. Handle the case where the
denominator is zero using a try-catch block.
#include <iostream>
using namespace std;
class Division {
public:
try {
if (denominator == 0) {
};
int main() {
Division div;
return 0;
OUTPUT:
Enter numerator: 10
Enter denominator: 2
Result: 5