C++ practice questions for assignment presentation
PratimaRai23
10 views
17 slides
Sep 16, 2025
Slide 1 of 17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
About This Presentation
coding cpp
Size: 902.78 KB
Language: en
Added: Sep 16, 2025
Slides: 17 pages
Slide Content
Pratima Kumari
PRN: 250850150061
C++ Assignment 2
Q.1
Create a class Student with:
public variables: name, age.
A method display() to print student details.
In main(), create a Student object, assign values directly, and display them.
(This shows that public members are accessible outside the class.)
int main()
{
// Creating Student object
Student s1;
// Assigning values directly to public variables
s1.name = "Pratima";
s1.age = 27;
// Displaying details
s1.display();
return 0;
}
OUTPUT
Q 2: Understanding private
Create a class BankAccount with:
private variables: accountNumber, balance.
public methods: setDetails(), getDetails().
In main(), try to directly access balance → observe error.
(This shows that private protects data from direct access.)
#include <iostream>
using namespace std;
class BankAccount {
private:
int accountNumber;
double balance;
public:
// Method to set account details
void setDetails(int accNo, double bal) {
accountNumber = accNo;
balance = bal;
}
// Setting account details using method
acc.setDetails(12345, 5000.75);
// Getting account details
acc.getDetails();
return 0;
}
OUTPUT
Q 3: Combining private and public
Create a class Rectangle with:
private variables: length, width.
public methods:
setDimensions() to assign values,
getArea() to return area.
In main(), demonstrate that dimensions cannot be accessed directly, but area can be
computed using methods.
#include <iostream>
using namespace std;
// Q3: Demonstrating private + public in a class
class Rectangle {
private:
double length;
double width;
public:
// Method to set dimensions
void setDimensions(double l, double w) {
length = l;
width = w;
}
// Method to calculate and return area
double getArea() {
return length * width;
}
};
int main() {
Rectangle rect;
// Setting values using method
rect.setDimensions(10.5, 4.2);
// Computing area using public method
cout << "Area of Rectangle: " << rect.getArea() << endl;
return 0;
}
OUTPUT
Q 4: Encapsulation in Practice
Create a class Employee with:
private variables: id, salary.
public methods:
setData() to set values,
displayData() to print details.
In main(), show how id and salary are hidden and only controlled via methods.
#include <iostream>
using namespace std;
// Q4: Encapsulation Example
class Employee {
private:
int id;
double salary;
public:
// Method to set employee details
void setData(int empId, double empSalary) {
// Setting values through public method
e1.setData(101, 55000.75);
// Displaying values using public method
e1.displayData();
return 0;
}
OUTPUT
Q 5: Small Challenge
Create a class Car with:
private variable: engineNumber.
public variables: brand, model.
public method showDetails().
In main(), let students try to access engineNumber
→ it should fail.
#include <iostream>
using namespace std;
class Car {
private:
string engineNumber; // Private variable
public:
string brand; // Public variables
string model;
// Method to set engine number (since it's private)
void setEngineNumber(string engNo) {
engineNumber = engNo;
int main() {
Car car1;
// Assign public members directly
car1.brand = "Toyota";
car1.model = "Corolla";
car1.setEngineNumber("ENG12345XYZ");
car1.showDetails();