0% found this document useful (0 votes)
24 views

C++ Practical File

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C++ Practical File

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Page |1

Practical - 1
WAP to implement Inline function

Code:

#include <iostream.h>

inline int add(int a, int b) {


return a + b;
}

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();

cout << "Original value: " << ref << endl;


ref = 20;
cout << "Updated value: " << num.getValue() << endl;

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) {}

friend void Teacher::gradeStudent(Student& student, int grade);


};

void Teacher::gradeStudent(Student& student, int grade) {


student.marks = grade;
cout << student.name << " has been graded " << grade << endl;
}

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 add(int a, int b) {


return a + b;
}

double add(double a, double b) {


return a + b;
}

int main() {
int sum1 = add(5, 10);
double sum2 = add(3.5, 2.5);

cout << "Sum of integers: " << sum1 << endl;


cout << "Sum of doubles: " << sum2 << endl;

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(const MyClass& other) {


data = other.data;
cout << "Copy Constructor called. Copied value: " << 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;
}
};

class Derived : public Base1, public Base2 {


public:
Derived() {
cout << "Derived 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();
};

cEmployee :: cEmployee(int i, int sal, int d, int m, int y)


{
mId = i;
mBasicSal = sal;
mBdate = cDate(d,m,y);
}
void cEmployee::display()
{
cout << "Id : " << mId << endl;
cout << "Salary :" <<mBasicSal << endl;
mBdate.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;

cout << "Before swapping: " << endl;


cout << "x = " << x << ", y = " << y << endl;

Swap(x, y);

cout << "After swapping: " << endl;


cout << "x = " << x << ", y = " << y << endl;

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;

double division(int a, int b) {


if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}

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) {}

// Function to display person details


void display() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};

int main() {
std::ofstream outFile("persons.dat", std::ios::binary);

// Writing objects to file


if (outFile.is_open()) {
Person person1("John", 25);
Person person2("Alice", 30);
P a g e | 19

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;
}

std::ifstream inFile("persons.dat", std::ios::binary);

// Reading objects from file


if (inFile.is_open()) {
Person readPerson1, readPerson2;

inFile.read(reinterpret_cast<char*>(&readPerson1), sizeof(Person));
inFile.read(reinterpret_cast<char*>(&readPerson2), sizeof(Person));

std::cout << "Read from file:" << std::endl;


readPerson1.display();
readPerson2.display();

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);
}

// Function to display data


void showdata() {
P a g e | 22

std::cout << "Name: " << name << std::endl;


std::cout << "Age: " << age << std::endl;
std::cout << "Address: " << address << std::endl;
}
};

int main() {
// Create an object of the Employee class
Employee emp;

// Get data from the user


emp.getdata();

// Display the entered data


std::cout << "\nEmployee Details:\n";
emp.showdata();

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);
}

// display complex number


void print() {
cout << real << " + i" << imag << endl;
}
};

int main() {
Complex c1(2.2, 3.3), c2(1.1, 2.2), c3;

cout << "First Complex number: ";


c1.print();
P a g e | 24

cout << "Second Complex number: ";


c2.print();

// 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) {}

// Overloading binary + without using friend function


MyClass operator+(const MyClass& obj) const {
return MyClass(value + obj.value);
}

// Function to display the value


void display() const {
std::cout << "Value: " << value << std::endl;
}
};

int main() {
// Create two objects of MyClass
MyClass obj1(5);
MyClass obj2(10);

// Add and display the result without using friend function


P a g e | 26

MyClass result = obj1 + obj2;


result.display();

return 0;
}

Result:

Code:

#include <iostream>

class MyClass {
private:
int value;

public:
// Constructor
MyClass(int val) : value(val) {}

// Friend function for overloading binary +


friend MyClass operator+(const MyClass& obj1, const MyClass& obj2);

// Function to display the value


P a g e | 27

void display() const {


std::cout << "Value: " << value << std::endl;
}
};

// Overloading binary + using friend function


MyClass operator+(const MyClass& obj1, const MyClass& obj2) {
return MyClass(obj1.value + obj2.value);
}

int main() {
// Create two objects of MyClass
MyClass obj1(5);
MyClass obj2(10);

// Add and display the result using friend function


MyClass result = obj1 + obj2;
result.display();

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");

// Check if the file is open


if (!inputFile.is_open()) {
std::cerr << "Error opening the file!" << std::endl;
return 1; // Return with an error code
}

char ch;
int digitCount = 0, alphabetCount = 0, spaceCount = 0;

// Read the file character by character


while (inputFile.get(ch)) {
if (isdigit(ch)) {
digitCount++;
} else if (isalpha(ch)) {
alphabetCount++;
} else if (isspace(ch)) {
spaceCount++;
}
}
P a g e | 29

// Close the file


inputFile.close();

// Display the counts


std::cout << "Digits: " << digitCount << std::endl;
std::cout << "Alphabets: " << alphabetCount << std::endl;
std::cout << "Spaces: " << spaceCount << std::endl;

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>

using namespace std;

class Integer
{
public:
int value;

Integer(int n) : value(n) {}
};

class String
{
public:
string value;

String(string str) : value(str) {}


};

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:

You might also like