0% found this document useful (0 votes)
8 views21 pages

Exp Oop

The document contains multiple C++ programs demonstrating various concepts such as classes, constructors, destructors, operator overloading, and inheritance. Each program showcases different functionalities like calculating squares and cubes, managing student and employee details, and implementing hierarchical and hybrid inheritance. The code examples illustrate object-oriented programming principles in C++.

Uploaded by

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

Exp Oop

The document contains multiple C++ programs demonstrating various concepts such as classes, constructors, destructors, operator overloading, and inheritance. Each program showcases different functionalities like calculating squares and cubes, managing student and employee details, and implementing hierarchical and hybrid inheritance. The code examples illustrate object-oriented programming principles in C++.

Uploaded by

wexiva7911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

EXP NO.

5(A)

#include <iostream>

using namespace std;

class Number {

private:

int num; // Data member to store the number

public:

// Default constructor to initialize

num Number() {

num = 5; // Assigning default value 5 to num

// Function to display the square of the number

void displaySquare() {

cout << "Square of " << num << " is: " << (num * num) << endl;

// Function to display the cube of the number

void displayCube() {

cout << "Cube of " << num << " is: " << (num * num * num) << endl;

};

int main() {

// Creating an object of class Number

Number n;

// Calling member functions to display square and cube

n.displaySquare();
n.displayCube();

return 0;

Output:
EXP NO 5(B)

#include <iostream>

using namespace std;

class Student {

private:

int rollNo;

string name;

long PRN;

float totalMarks;

float percentage;

public:

// Parameterized constructor to initialize the data

members Student(int r, string n, long p, float marks) {

rollNo = r;

name = n;

PRN = p;

totalMarks = marks;

// Function to calculate

percentage void

calculatePercentage() {

percentage = (totalMarks / 500) * 100; // Assuming total marks out of 500

// Function to display student

details void displayDetails() {

calculatePercentage(); // First calculate the

percentage cout << "Student Details:" << endl;

cout << "Roll No: " << rollNo << endl;


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

cout << "PRN: " << PRN << endl;

cout << "Total Marks: " << totalMarks << "/500" << endl;

cout << "Percentage: " << percentage << "%" << endl;

};

int main() {

// Creating an object of Student class using parameterized constructor

Student s(101, "Mohammad Sayeed", 1234567890, 450);

// Displaying student details and

percentage s.displayDetails();

return 0;

Output:
EXP NO 5( C )

#include <iostream>

using namespace std;

class Book {

private:

string bookName;

string authorName;

float price;

long ISBN;

public:

// Parameterized constructor to initialize the data

members Book(string bName, string aName, float p, long

isbn) {

bookName = bName;

authorName = aName;

price = p;

ISBN = isbn;

// Copy constructor to copy data from another Book object

Book(const Book &b) {

bookName = b.bookName;

authorName = b.authorName;

price = b.price;

ISBN = b.ISBN;

// Function to display book

details void displayDetails() {

cout << "Book Name: " << bookName << endl;


cout << "Author Name: " << authorName << endl;

cout << "Price: $" << price << endl;

cout << "ISBN: " << ISBN << endl;

};

int main() {

// Creating an object of Book class using parameterized constructor

Book book1("C++ Programming", "Bjarne Stroustrup", 49.99, 1234567890123);

// Using the copy constructor to copy details from book1 to book2

Book book2 = book1;

// Displaying details of both books

cout << "Details of Book 1:" <<

endl; book1.displayDetails();

cout << "\nDetails of Book 2 (Copied from Book 1):" << endl;

book2.displayDetails();

return 0;

Output:
EXP NO : 6 ( A )

#include <iostream>

using namespace std;

class Employee {

private:

int emp_id;

string name;

string designation;

float salary;

public:

// Default

constructor

Employee() {

emp_id = 0;

name = "Unknown";

designation = "Not

Assigned"; salary = 0.0;

cout << "Default Constructor called for Employee ID: " << emp_id << endl;

// Parameterized constructor

Employee(int id, string emp_name, string desig, float sal)

{ emp_id = id;

name = emp_name;

designation = desig;

salary = sal;

cout << "Parameterized Constructor called for Employee ID: " << emp_id << endl;

// Destructor to release objects


~Employee() {

cout << "Destructor called for Employee ID: " << emp_id << endl;

// Function to display employee

details void displayDetails() {

cout << "Employee ID: " << emp_id << endl;

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

cout << "Designation: " << designation << endl;

cout << "Salary: $" << salary << endl;

};

int main() {

// Creating an object using the default

constructor Employee emp1;

cout << "\nDetails of Employee 1 (Default Constructor):" << endl;

emp1.displayDetails();

cout << endl;

// Creating an object using the parameterized constructor

Employee emp2(101, "Mohammad Sayeed", "Software Engineer", 75000.75);

cout << "Details of Employee 2 (Parameterized Constructor):" << endl;

emp2.displayDetails();

// Destructor will be called automatically when program ends or objects go out of scope

return 0;

}
Output:
EXP NO 6 ( B)

#include <iostream>

using namespace std;

class Number {

private:

int value;

public:

// Constructor

Number(int v = 0) : value(v) {}

// Unary minus (-) operator

overloading Number operator-() {

return Number(-value);

// Binary plus (+) operator overloading

Number operator+(const Number& obj)

return Number(value + obj.value);

// Function to display

value void display() {

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

};

int main() {

Number n1(10), n2(20);


// Unary operator

overloading Number n3 = -n1;

cout << "Unary minus of n1: ";

n3.display();

// Binary operator overloading

Number n4 = n1 + n2;

cout << "Sum of n1 and n2: ";

n4.display();

return 0;

Output:
EXP NO : 7 (A)

#include <iostream>

using namespace std;

// Base class

class Person {

protected:

string name;

int age;

public:

// Function to read personal

details void readPersonDetails() {

cout << "Enter name:

"; getline(cin, name);

cout << "Enter age: ";

cin >> age;

// Function to display personal

details void displayPersonDetails() {

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

cout << "Age: " << age << endl;

};

// Derived class

class Student : public Person

{ private:

int roll_no;
public:

// Function to read student

details void readStudentDetails() {

readPersonDetails(); // Inherited function

cout << "Enter roll number: ";

cin >> roll_no;

// Function to display student

details void displayStudentDetails() {

displayPersonDetails(); // Inherited function

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

};

int main() {

Student s1;

// Read and display student

details s1.readStudentDetails();

cout << "\nStudent Information:\

n"; s1.displayStudentDetails();

return 0;

}
Output:
EXP NO: 7( B)

#include <iostream>

using namespace std;

// Base class 1

class Person {

protected:

string name;

int age;

public:

// Function to read personal

details void readPersonDetails() {

cout << "Enter name:

"; getline(cin, name);

cout << "Enter age: ";

cin >> age;

cin.ignore(); // To handle input buffer

// Function to display personal

details void displayPersonDetails() {

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

cout << "Age: " << age << endl;

};

// Base class 2

class Company {

protected:

string company_name;
public:

// Function to read company

details void readCompanyDetails() {

cout << "Enter company name:

"; getline(cin, company_name);

// Function to display company

details void displayCompanyDetails() {

cout << "Company Name: " << company_name << endl;

};

// Derived class

class Employee : public Person, public Company

{ private:

int emp_id;

public:

// Function to read employee

details void readEmployeeDetails() {

readPersonDetails(); // Inherited from Person

readCompanyDetails(); // Inherited from

Company cout << "Enter employee ID: ";

cin >> emp_id;

// Function to display employee

details void displayEmployeeDetails() {

displayPersonDetails(); // Inherited from Person


displayCompanyDetails(); // Inherited from Company

cout << "Employee ID: " << emp_id << endl;

};

int main() {

Employee emp;

// Read and display employee

details emp.readEmployeeDetails();

cout << "\nEmployee Information:\

n"; emp.displayEmployeeDetails();

return 0;

Output:
EXP NO: 7(C)

#include <iostream>

using namespace std;

// Base class

class Person

{ protected:

string name;

int age;

public:

void readPersonDetails()

{ cout << "Enter name:

"; getline(cin, name);

cout << "Enter age:

"; cin >> age;

cin.ignore(); // Clear the input buffer

void displayPersonDetails() {

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

cout << "Age: " << age << endl;

};

// Derived class 1 (Hierarchical Inheritance)

class Employee : public Person {

protected:

int emp_id;

public:
void readEmployeeDetails()

{ readPersonDetails();

cout << "Enter Employee ID: ";

cin >> emp_id;

cin.ignore();

void displayEmployeeDetails() {

displayPersonDetails();

cout << "Employee ID: " << emp_id << endl;

};

// Derived class 2 (Hierarchical Inheritance)

class Student : public Person {

protected:

int student_id;

public:

void readStudentDetails()

{ readPersonDetails();

cout << "Enter Student ID:

"; cin >> student_id;

cin.ignore();

void displayStudentDetails()

{ displayPersonDetails();

cout << "Student ID: " << student_id << endl;

};
// Derived class 3 (Hybrid Inheritance)

class PartTimeEmployee : public Employee, public Student {

private:

int work_hours;

public:

void readPartTimeEmployeeDetails() {

readEmployeeDetails();

cout << "Enter work hours:

"; cin >> work_hours;

void displayPartTimeEmployeeDetails() {

displayEmployeeDetails();

cout << "Work hours: " << work_hours << endl;

};

int main() {

PartTimeEmployee pt_emp;

// Read and display part-time employee details

pt_emp.readPartTimeEmployeeDetails();

cout << "\nPart-time Employee Information:\n";

pt_emp.displayPartTimeEmployeeDetails();

return 0;

}
Output:

You might also like