Tutorial
Name – Rishabh Nirmalkar
Reg no. – 25BCG10026
1. Write a C++ program that compares all three access specifiers.
• Create a class with:
a) one private member
b) one protected member
c) one public member
• Demonstrate accessibility:
a) inside the class
b) inside derived class
c) outside the class
Code :
#include <iostream>
using namespace std;
class Base {
private:
int privateValue;
protected:
int protectedValue;
public:
int publicValue;
Base() : privateValue(10), protectedValue(20), publicValue(30) {}
void showInsideClass() {
cout << "Inside Base class:\n";
cout << " privateValue = " << privateValue << "\n";
cout << " protectedValue = " << protectedValue << "\n";
cout << " publicValue = " << publicValue << "\n";
}
};
class Derived : public Base {
public:
void showInsideDerived() {
cout << "Inside Derived class:\n";
// privateValue is not accessible here because it is private in Base
// cout << " privateValue = " << privateValue << "\n";
cout << " protectedValue = " << protectedValue << "\n";
cout << " publicValue = " << publicValue << "\n";
}
};
int main() {
Base base;
Derived derived;
[Link]();
cout << "\n";
[Link]();
cout << "\n";
cout << "Outside the class:\n";
// privateValue and protectedValue are not accessible from outside the class
// cout << " privateValue = " << [Link] << "\n";
// cout << " protectedValue = " << [Link] << "\n";
cout << " publicValue = " << [Link] << "\n";
return 0;
}
Output :
2. Write a C++ program to demonstrate public inheritance.
a) Create a base class Vehicle
b) Create a derived class Car
c) Access inherited public members through derived class objects.
d) Explain member visibility after public inheritance.
Code :
#include <iostream>
using namespace std;
class Vehicle {
public:
string make;
string model;
int year;
Vehicle(const string& make_, const string& model_, int year_)
: make(make_), model(model_), year(year_) {}
void displayInfo() const {
cout << "Vehicle info:\n";
cout << " Make : " << make << "\n";
cout << " Model: " << model << "\n";
cout << " Year : " << year << "\n";
}
};
class Car : public Vehicle {
public:
int numberOfDoors;
Car(const string& make_, const string& model_, int year_, int doors)
: Vehicle(make_, model_, year_), numberOfDoors(doors) {}
void displayCarInfo() const {
cout << "Car info (derived from Vehicle):\n";
displayInfo();
cout << " Doors: " << numberOfDoors << "\n";
}
};
int main() {
Car myCar("Toyota", "Camry", 2024, 4);
// Access inherited public members through derived class object
cout << "Accessing inherited members from derived object:\n";
cout << " make = " << [Link] << "\n";
cout << " model = " << [Link] << "\n";
cout << " year = " << [Link] << "\n";
cout << " doors = " << [Link] << "\n\n";
[Link]();
return 0;
}
Output :
3. Write a C++ program to find the maximum of:
a) Two integers
b) Three integers (using function overloading)
Code :
#include <iostream>
using namespace std;
int maximum(int a, int b) {
return (a > b) ? a : b;
}
int maximum(int a, int b, int c) {
return maximum(maximum(a, b), c);
}
int main() {
int x, y, z;
cout << "Enter two integers: ";
cin >> x >> y;
cout << "Maximum of " << x << " and " << y << " is " << maximum(x, y) << ".\n\n";
cout << "Enter three integers: ";
cin >> x >> y >> z;
cout << "Maximum of " << x << ", " << y << " and " << z << " is " << maximum(x, y, z) << ".\
n";
return 0;
}
Output :
4. Write a C++ program to store n integers in a vector, sort them in ascending order and
search for a given element using STL algorithms.
Code :
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cout << "Enter the number of integers: ";
cin >> n;
vector<int> values;
[Link](n);
cout << "Enter " << n << " integers:\n";
for (int i = 0; i < n; ++i) {
int value;
cin >> value;
values.push_back(value);
}
sort([Link](), [Link]());
cout << "\nSorted values in ascending order:\n";
for (int value : values) {
cout << value << " ";
}
cout << "\n\n";
int target;
cout << "Enter element to search: ";
cin >> target;
bool found = binary_search([Link](), [Link](), target);
if (found) {
auto it = lower_bound([Link](), [Link](), target);
cout << "Element " << target << " found at sorted index " << distance([Link](), it)
<< ".\n";
} else {
cout << "Element " << target << " not found.\n";
}
return 0;
}
Output :
5. Suppose you have a piggie bank with an initial amount of $50, and you have to add some
more amount to it. Create a class ‘AddAmount’ with a data member named ‘amount’ with
an initial value of $50. Now make two constructors of this class as follows:
a) Without any parameter – no amount will be added to the piggie bank
b) Having a parameter, which is the amount that will be added to the piggie bank
Create an object of the ‘AddAmount’ class and display the final amount in the piggie bank.
Code :
#include <iostream>
using namespace std;
class AddAmount {
public:
int amount;
// Default constructor: no amount added
AddAmount() : amount(50) {}
// Parameterized constructor: adds the given amount to the piggie bank
AddAmount(int addedAmount) : amount(50 + addedAmount) {}
void display() const {
cout << "Final amount in piggie bank: $" << amount << "\n";
}
};
int main() {
AddAmount piggie1; // no extra amount added
cout << "Using default constructor (no amount added):\n";
[Link]();
cout << "\nUsing parameterized constructor (add $25):\n";
AddAmount piggie2(25); // add 25 dollars to the piggie bank
[Link]();
return 0;
}
Output :
6. Make a class named Fruit with a data member to calculate the number of fruits in a
basket. Create two other classes named Apples and Mangoes to calculate the number of
apples and mangoes in the basket. Print the number of fruits of each type and the total
number of fruits in the basket.
Code :
#include <iostream>
using namespace std;
class Fruit {
protected:
int totalFruits;
public:
Fruit(int total) : totalFruits(total) {}
int getTotalFruits() const {
return totalFruits;
}
};
class Apples : public Fruit {
int appleCount;
public:
Apples(int count) : Fruit(count), appleCount(count) {}
int getAppleCount() const {
return appleCount;
}
};
class Mangoes : public Fruit {
int mangoCount;
public:
Mangoes(int count) : Fruit(count), mangoCount(count) {}
int getMangoCount() const {
return mangoCount;
}
};
int main() {
int apples, mangoes;
cout << "Enter number of apples: ";
cin >> apples;
cout << "Enter number of mangoes: ";
cin >> mangoes;
Apples appleBasket(apples);
Mangoes mangoBasket(mangoes);
int total = [Link]() + [Link]();
cout << "Number of apples: " << [Link]() << "\n";
cout << "Number of mangoes: " << [Link]() << "\n";
cout << "Total number of fruits: " << total << "\n";
return 0;
}
Output :
7. Write a C++ program to create a file and store multiple student records sequentially. Then
read and display the contents.
Code :
#include <fstream>
#include <iostream>
#include <string>
#include <limits>
using namespace std;
struct Student {
int id;
char name[50];
float marks;
};
int main() {
const char* filename = "[Link]";
int n;
cout << "Enter number of students: ";
cin >> n;
[Link]( numeric_limits<streamsize >::max(), '\n');
ofstream outFile(filename, ios::binary);
if (!outFile) {
cerr << "Unable to create file " << filename << "\n";
return 1;
}
for (int i = 0; i < n; ++i) {
Student s;
cout << "\nStudent " << (i + 1) << " ID: ";
cin >> [Link];
[Link](numeric_limits<streamsize>::max(), '\n');
cout << "Student " << (i + 1) << " Name: ";
[Link]([Link], sizeof([Link]));
cout << "Student " << (i + 1) << " Marks: ";
cin >> [Link];
[Link](numeric_limits<streamsize>::max(), '\n');
[Link](reinterpret_cast<const char*>(&s), sizeof(s));
}
[Link]();
cout << "\nReading stored student records...\n\n";
ifstream inFile(filename, ios::binary);
if (!inFile) {
cerr << "Unable to open file " << filename << " for reading\n";
return 1;
}
Student s;
int index = 1;
while ([Link](reinterpret_cast<char*>(&s), sizeof(s))) {
cout << "Student " << index++ << ":\n";
cout << " ID : " << [Link] << "\n";
cout << " Name : " << [Link] << "\n";
cout << " Marks : " << [Link] << "\n\n";
}
[Link]();
return 0;
}
Output :
8. Write a C++ program using a function template to swap two values. Swap: a) integers b)
characters c) floating-point values Display values before and after swapping.
Code :
#include <iostream>
using namespace std;
template <typename T>
void swapValues(T& a, T& b) {
T temp = a;
a = b;
b = temp;
}
int main() {
int i1 = 5, i2 = 10;
char c1 = 'A', c2 = 'Z';
double d1 = 3.14, d2 = 6.28;
cout << "Before swapping integers: i1 = " << i1 << ", i2 = " << i2 << "\n";
swapValues(i1, i2);
cout << "After swapping integers: i1 = " << i1 << ", i2 = " << i2 << "\n\n";
cout << "Before swapping characters: c1 = " << c1 << ", c2 = " << c2 << "\n";
swapValues(c1, c2);
cout << "After swapping characters: c1 = " << c1 << ", c2 = " << c2 << "\n\n";
cout << "Before swapping floats: d1 = " << d1 << ", d2 = " << d2 << "\n";
swapValues(d1, d2);
cout << "After swapping floats: d1 = " << d1 << ", d2 = " << d2 << "\n";
return 0;
}
Output :
9. Write a C++ program using a class template to implement a stack. Implement: a. push() b.
pop() c. display() Demonstrate stack operations for integers.
Code :
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Stack {
private:
vector<T> elements;
public:
void push(const T& value) {
elements.push_back(value);
}
void pop() {
if ([Link]()) {
cout << "Stack is empty. Cannot pop.\n";
return;
}
elements.pop_back();
}
void display() const {
if ([Link]()) {
cout << "Stack is empty.\n";
return;
}
cout << "Stack contents (top to bottom): ";
for (auto it = [Link](); it != [Link](); ++it) {
cout << *it << " ";
}
cout << "\n";
}
};
int main() {
Stack<int> intStack;
cout << "Pushing integers: 10, 20, 30" << "\n";
[Link](10);
[Link](20);
[Link](30);
[Link]();
cout << "\nPopping one value...\n";
[Link]();
[Link]();
cout << "\nPushing 40...\n";
[Link](40);
[Link]();
return 0;
}
Output :
10. Write a C++ program to demonstrate the use of a virtual function.
Create a base class Animal Create derived classes:
a. Dog
b. Cat
Define a virtual function sound() in the base class.
Override the function in derived classes. Use base class pointer to call derived class functions.
Code :
#include <iostream>
#include <memory>
using namespace std;
class Animal {
public:
virtual void sound() const {
cout << "Animal makes a sound." << endl;
}
virtual ~Animal() = default;
};
class Dog : public Animal {
public:
void sound() const override {
cout << "Dog says: Woof!" << endl;
}
};
class Cat : public Animal {
public:
void sound() const override {
cout << "Cat says: Meow!" << endl;
}
};
int main() {
unique_ptr<Animal> animalPtr;
animalPtr = make_unique<Dog>();
cout << "Using Animal pointer to call Dog sound(): ";
animalPtr->sound();
animalPtr = make_unique<Cat>();
cout << "Using Animal pointer to call Cat sound(): ";
animalPtr->sound();
return 0;
}
Output :