0% found this document useful (0 votes)
13 views25 pages

C++ Programming Lab Manual Examples

The document contains a series of C++ programming exercises covering various concepts such as finding the largest of three numbers, sorting elements, using classes for student and bank account details, function overloading, operator overloading, multiple inheritance, constructors in derived classes, file handling, and binary file operations. Each exercise includes code snippets demonstrating the implementation of the respective concepts. The document serves as a lab manual for practicing object-oriented programming in C++.

Uploaded by

nidhi.csd26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

C++ Programming Lab Manual Examples

The document contains a series of C++ programming exercises covering various concepts such as finding the largest of three numbers, sorting elements, using classes for student and bank account details, function overloading, operator overloading, multiple inheritance, constructors in derived classes, file handling, and binary file operations. Each exercise includes code snippets demonstrating the implementation of the respective concepts. The document serves as a lab manual for practicing object-oriented programming in C++.

Uploaded by

nidhi.csd26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOP’S

LAB MANUAL
1. Write a c++ program to find the largest of three numbers

#include <iostream>

using namespace std;

int main() {

int num1, num2, num3;

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

int largest = num1;

if (num2 > largest) {

largest = num2;

if (num3 > largest) {

largest = num3;

cout << "The largest number is: " << largest << endl;

return 0;

}
[Link] a c++ program to sort the elements in ascending and descending order

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

int main() {

vector<int> numbers = {5, 2, 9, 1, 7};

// Sort in ascending order

sort([Link](), [Link]());

cout << "Ascending order: ";

for (int num : numbers) {

cout << num << " ";

cout << endl;

// Sort in descending order

reverse([Link](), [Link]());

cout << "Descending order: ";

for (int num : numbers) {

cout << num << " ";

}
cout << endl;

return 0;

}
[Link] a c++ program using classes to display student name roll number marks
obtained in two subjects and total score of student

#include <iostream>

#include <string>

using namespace std;

class Student {

public:

string name;

int rollNumber;

int subject1Marks;

int subject2Marks;

Student(string name, int rollNumber, int subject1Marks, int subject2Marks) {

this->name = name;

this->rollNumber = rollNumber;

this->subject1Marks = subject1Marks;

this->subject2Marks = subject2Marks;

int calculateTotalMarks() {

return subject1Marks + subject2Marks;

}
void displayStudentDetails() {

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

cout << "Roll Number: " << rollNumber << endl;

cout << "Subject 1 Marks: " << subject1Marks << endl;

cout << "Subject 2 Marks: " << subject2Marks << endl;

cout << "Total Marks: " << calculateTotalMarks() << endl;

};

int main() {

string name;

int rollNumber, subject1Marks, subject2Marks;

cout << "Enter Student Name: ";

getline(cin, name);

cout << "Enter Roll Number: ";

cin >> rollNumber;

cout << "Enter Marks for Subject 1: ";

cin >> subject1Marks;

cout << "Enter Marks for Subject 2: ";

cin >> subject2Marks;

Student student(name, rollNumber, subject1Marks, subject2Marks);


[Link]();

return 0;

}
[Link] a c plus plus program for a bank employee to print name of the employee
account number and balance print invalid balance amount is lesser than 500 display the
same also display the balance after withdrawal and deposit

#include <iostream>

#include <string>

using namespace std;

class BankAccount {

public:

string name;

int accountNumber;

double balance;

BankAccount(string name, int accountNumber, double balance) {

this->name = name;

this->accountNumber = accountNumber;

this->balance = balance;

void displayAccountDetails() {

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

cout << "Account Number: " << accountNumber << endl;

cout << "Balance: " << balance << endl;

}
void deposit(double amount) {

balance += amount;

cout << "Deposit successful. New balance: " << balance << endl;

void withdraw(double amount) {

if (amount > balance) {

cout << "Insufficient balance." << endl;

} else if (balance - amount < 500) {

cout << "Invalid withdrawal. Minimum balance must be 500." << endl;

} else {

balance -= amount;

cout << "Withdrawal successful. New balance: " << balance << endl;

};

int main() {

string name;

int accountNumber;

double balance;

cout << "Enter Employee Name: ";

getline(cin, name);
cout << "Enter Account Number: ";

cin >> accountNumber;

cout << "Enter Initial Balance: ";

cin >> balance;

BankAccount account(name, accountNumber, balance);

[Link]();

char choice;

double amount;

do {

cout << "\nChoose an option:\n";

cout << "1. Deposit\n";

cout << "2. Withdraw\n";

cout << "3. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case '1':

cout << "Enter deposit amount: ";

cin >> amount;

[Link](amount);

break;
case '2':

cout << "Enter withdrawal amount: ";

cin >> amount;

[Link](amount);

break;

case '3':

cout << "Exiting...\n";

break;

default:

cout << "Invalid choice. Please try again.\n";

} while (choice != '3');

return 0;

}
[Link] a c plus plus program to demonstrate function overloading for the following
prototype

add(int a, int b)

add(double a,double b)

#include <iostream>

using namespace std;

// Function overloading: Same function name, different parameters

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

int main() {

int intResult = add(5, 10);

double doubleResult = add(3.14, 2.718);

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

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


return 0;

}
[Link] a c++ program using operator overloading for overloading unary minus operator

#include <iostream>

using namespace std;

class Complex {

public:

double real, imag;

Complex(double r = 0, double i = 0) : real(r), imag(i) {}

Complex operator-() {

return Complex(-real, -imag);

void display() {

cout << real << " + " << imag << "i" << endl;

};

int main() {

Complex c1(3, 4);

Complex c2 = -c1;

cout << "Original complex number: ";


[Link]();

cout << "Negated complex number: ";

[Link]();

return 0;

}
[Link] a c++ program to implement multiple inheritance for performing arithmetic
operation of two numbers

#include <iostream>

using namespace std;

class Number {

public:

int num;

Number(int num) : num(num) {}

int getNum() {

return num;

};

class Addition {

public:

int add(int a, int b) {

return a + b;

};

class Subtraction {

public:
int subtract(int a, int b) {

return a - b;

};

class Arithmetic : public Number, public Addition, public Subtraction {

public:

Arithmetic(int num) : Number(num) {}

void performOperations() {

int num1 = getNum();

int num2 = 5; // Assuming a fixed second number for simplicity

int sum = add(num1, num2);

int difference = subtract(num1, num2);

cout << "Number 1: " << num1 << endl;

cout << "Number 2: " << num2 << endl;

cout << "Sum: " << sum << endl;

cout << "Difference: " << difference << endl;

};

int main() {

int num;
cout << "Enter a number: ";

cin >> num;

Arithmetic arithmetic(num);

[Link]();

return 0;

}
[Link] a c plus plus program using constructor in derived class to initialize alpha beta
and gamma and display corresponding values

#include <iostream>

using namespace std;

class Base {

public:

int alpha;

Base(int a) : alpha(a) {}

};

class Derived1 : public Base {

public:

int beta;

Derived1(int a, int b) : Base(a), beta(b) {}

};

class Derived2 : public Derived1 {

public:

int gamma;
Derived2(int a, int b, int c) : Derived1(a, b), gamma(c) {}

void display() {

cout << "Alpha: " << alpha << endl;

cout << "Beta: " << beta << endl;

cout << "Gamma: " << gamma << endl;

};

int main() {

Derived2 d(10, 20, 30);

[Link]();

return 0;

}
[Link] a C++ program to create a text file check file created or not if created it will write
some text in to the file and then read the text from the file

#include <iostream>

#include <fstream>

using namespace std;

int main() {

ofstream outfile("[Link]");

if (outfile.is_open()) {

cout << "File created successfully!" << endl;

// Write some text to the file

outfile << "This is some text written to the file." << endl;

outfile << "Hello, world!" << endl;

[Link]();

} else {

cout << "Error creating file." << endl;

return 1;

// Read from the file


ifstream infile("[Link]");

if (infile.is_open()) {

string line;

while (getline(infile, line)) {

cout << line << endl;

[Link]();

} else {

cout << "Error opening file for reading." << endl;

return 1;

return 0;

}
[Link] a c++ program to write and read time in/from binary file using fstream

#include <iostream>

#include <fstream>

#include <ctime>

using namespace std;

struct Time {

int hours;

int minutes;

int seconds;

};

int main() {

Time time;

// Get current time

time_t now = time(0);

tm *t = localtime(&now);

[Link] = t->tm_hour;

[Link] = t->tm_min;

[Link] = t->tm_sec;
// Write time to binary file

ofstream outfile("[Link]", ios::binary);

if (outfile.is_open()) {

[Link](reinterpret_cast<char*>(&time), sizeof(time));

cout << "Time written to file successfully." << endl;

[Link]();

} else {

cout << "Error opening file for writing." << endl;

return 1;

// Read time from binary file

ifstream infile("[Link]", ios::binary);

if (infile.is_open()) {

[Link](reinterpret_cast<char*>(&time), sizeof(time));

cout << "Time read from file: " << [Link] << ":" << [Link] << ":" <<
[Link] << endl;

[Link]();

} else {

cout << "Error opening file for reading." << endl;

return 1;

return 0;
}

You might also like