C++ Practical File
C++ Practical File
Practical - 1
WAP to implement Inline function
Code:
#include <iostream.h>
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
int sum = add(num1, num2);
cout << "Sum: " << sum << endl;
return 0;
}
Result:
Page |2
Practical - 2
WAP to implement call by reference and return by reference using class.
Code:
#include <iostream>
using namespace std;
class Number {
private:
int value;
public:
Number(int val) : value(val) {}
int& getValue() {
return value;
}
};
int main() {
Number num(10);
int& ref = num.getValue();
return 0;
}
Page |3
Result:
Page |4
Practical - 3
WAP to implement friend function by taking some real life example
Code:
#include <iostream>
using namespace std;
class Student;
class Teacher {
public:
void gradeStudent(Student& student, int grade);
};
class Student {
private:
string name;
int marks;
public:
Student(string n) : name(n), marks(0) {}
int main() {
Student s1("Alice");
Page |5
Teacher t1;
t1.gradeStudent(s1, 95);
return 0;
}
Result:
Page |6
Practical - 4
WAP to implement Function Overloading
Code:
#include <iostream>
using namespace std;
int main() {
int sum1 = add(5, 10);
double sum2 = add(3.5, 2.5);
return 0;
}
Page |7
Result:
Page |8
Practical - 5
WAP to implement Parameterized Constructor, Copy Constructor and
Destructor
Code:
#include <iostream>
using namespace std;
class MyClass {
public:
int data;
MyClass(int value) {
data = value;
cout << "Parameterized Constructor called. Value set to " << data << endl;
}
~MyClass() {
cout << "Destructor called. Value " << data << " destroyed." << endl;
Page |9
}
};
int main() {
MyClass obj1(10); // Parameterized Constructor
MyClass obj2(obj1); // Copy Constructor
return 0;
}
Result:
P a g e | 10
Practical - 6
WAP to show the usage of constructor in base and derived classes, in
multiple inheritance
Code:
#include <iostream>
using namespace std;
class Base1 {
public:
Base1() {
cout << "Base1 Constructor" << endl;
}
};
class Base2 {
public:
Base2() {
cout << "Base2 Constructor" << endl;
}
};
int main() {
Derived d; // Calls Base1 and Base2 constructors, then Derived
constructor
P a g e | 11
return 0;
}
Result:
P a g e | 12
Practical - 7
WAP to show the implementation of containership.
Code:
#include<iostream>
using namespace std;
class cDate
{
int mDay,mMonth,mYear;
public:
cDate()
{
mDay = 10;
mMonth = 11;
mYear = 1999;
}
cDate(int d,int m ,int y)
{
mDay = d;
mMonth = m;
mYear = y;
}
void display()
{
cout << "day" << mDay << endl;
cout <<"Month" << mMonth << endl;
cout << "Year" << mYear << endl;
}
};
// Container class
class cEmployee
P a g e | 13
{
protected:
int mId;
int mBasicSal;
// Contained Object
cDate mBdate;
public:
cEmployee()
{
mId = 1;
mBasicSal = 10000;
mBdate = cDate();
}
cEmployee(int, int, int, int, int);
void display();
};
int main()
{
// Default constructor call
cEmployee e1;
P a g e | 14
e1.display();
// Parameterized constructor called
cEmployee e2(2,20000,11,11,1999);
e2.display();
return 0;
}
Result:
P a g e | 15
Practical – 8
WAP to show swapping using template function (Generic)
Code:
#include<iostream>
using namespace std;
template<class T>
void Swap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int main() {
int x = 5;
int y = 10;
Swap(x, y);
return 0;
}
P a g e | 16
Result:
P a g e | 17
Practical - 9
WAP to implement „Exception Handling‟
Code:
#include <iostream>
using namespace std;
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
Result:
P a g e | 18
Practical - 10
WAP to read and write values through object using file handling
Code:
#include <iostream>
#include <fstream>
class Person {
public:
std::string name;
int age;
// Default constructor
Person() : name(""), age(0) {}
// Parameterized constructor
Person(const std::string& n, int a) : name(n), age(a) {}
int main() {
std::ofstream outFile("persons.dat", std::ios::binary);
outFile.write(reinterpret_cast<char*>(&person1), sizeof(Person));
outFile.write(reinterpret_cast<char*>(&person2), sizeof(Person));
outFile.close();
std::cout << "Objects written to file successfully." << std::endl;
} else {
std::cerr << "Error opening file for writing." << std::endl;
return 1;
}
inFile.read(reinterpret_cast<char*>(&readPerson1), sizeof(Person));
inFile.read(reinterpret_cast<char*>(&readPerson2), sizeof(Person));
inFile.close();
} else {
std::cerr << "Error opening file for reading." << std::endl;
return 1;
}
return 0;
}
P a g e | 20
Result:
P a g e | 21
Practical - 11
Create a class employee which have name, age and address of employee,
include functions getdata() and showdata(), getdata() takes the input from
the user, showdata() display the data in following format:
Name:
Age:
Address:
Code:
#include <iostream>
#include <string>
class Employee {
private:
std::string name;
int age;
std::string address;
public:
// Function to get data from the user
void getdata() {
std::cout << "Enter Name: ";
std::getline(std::cin, name); // Using getline to read the entire line
std::cout << "Enter Age: ";
std::cin >> age;
std::cin.ignore(); // Ignore the newline character left in the buffer
std::cout << "Enter Address: ";
std::getline(std::cin, address);
}
int main() {
// Create an object of the Employee class
Employee emp;
return 0;
}
Result:
P a g e | 23
Practical - 12
WAP to add and subtract two complex numbers using classes
Code:
#include<iostream>
using namespace std;
class Complex {
private:
double real, imag;
public:
// constructor
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
// subtraction
Complex operator -(Complex const &obj) {
return Complex(real - obj.real, imag - obj.imag);
}
int main() {
Complex c1(2.2, 3.3), c2(1.1, 2.2), c3;
// subtraction
c3 = c1 - c2;
cout << "Subtraction of two complex numbers: ";
c3.print();
return 0;
}
Result:
P a g e | 25
Practical - 13
Write program to overload Binary + to add two similar types of objects.
(Both with and without using friend functions)
Code:
#include <iostream>
class MyClass {
private:
int value;
public:
// Constructor
MyClass(int val) : value(val) {}
int main() {
// Create two objects of MyClass
MyClass obj1(5);
MyClass obj2(10);
return 0;
}
Result:
Code:
#include <iostream>
class MyClass {
private:
int value;
public:
// Constructor
MyClass(int val) : value(val) {}
int main() {
// Create two objects of MyClass
MyClass obj1(5);
MyClass obj2(10);
return 0;
}
Result:
P a g e | 28
Practical - 14
WAP to count digits, alphabets and spaces, stored in a text file, using
streams
Code:
#include <iostream>
#include <fstream>
#include <cctype>
int main() {
// Open the text file for reading
std::ifstream inputFile("example.txt");
char ch;
int digitCount = 0, alphabetCount = 0, spaceCount = 0;
return 0;
}
Result:
P a g e | 30
Practical - 15
WAP to implement conversion of primitive data type to user defined data
types.
Code:
#include<iostream>
#include<fstream>
#include<sstream>
class Integer
{
public:
int value;
Integer(int n) : value(n) {}
};
class String
{
public:
string value;
template<typename T>
void convertAndDisplay(T val)
{
cout << val << endl;
}
P a g e | 31
int main()
{
int i = 10;
string str = "Hello World";
double d = 3.14;
convertAndDisplay(i);
convertAndDisplay(str);
convertAndDisplay(d);
return 0;
}
Result: