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

Lab 10

The document discusses object oriented programming concepts like function overloading, inheritance, polymorphism and abstract classes through code examples. It contains code for overloading the sum() function, defining base and derived classes for employees and students, and implementing abstract and derived account classes. The output from running the code is also included.

Uploaded by

menaelshahzadi5
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)
10 views

Lab 10

The document discusses object oriented programming concepts like function overloading, inheritance, polymorphism and abstract classes through code examples. It contains code for overloading the sum() function, defining base and derived classes for employees and students, and implementing abstract and derived account classes. The output from running the code is also included.

Uploaded by

menaelshahzadi5
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/ 14

NAME : MENAEL SHAHZADI

REG NO. B23F0233CYS021


SUBMITTED TO : MAM LAIBA SOHAIL
LAB REORT NO. 11
SUBJECT OBJECT ORIENTED PROGRAMMING
QUESTION NO. 1:

CODE:
#include <iostream>

// Function to calculate the sum of two numbers

int sum(int a, int b) {

return a + b;

// Function to calculate the sum of three numbers

int sum(int a, int b, int c) {

return a + b + c;

int main() {

// Example usage

int result1 = sum(5, 3); // Sum of two numbers

std::cout << "Sum of two numbers: " << result1 << std::endl;
int result2 = sum(2, 4, 6); // Sum of three numbers

std::cout << "Sum of three numbers: " << result2 << std::endl;

return 0;

OUTPUT:

QUESTION NO. 2:

CODE:
#include <iostream>
#include <string>

// Base class Employee

class Employee {

protected:

std::string name;

public:

// Constructor

Employee(const std::string& name) : name(name) {}

// Virtual function to display employee's name

virtual void display() {

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

};

// Derived class Manager

class Manager : public Employee {

private:
std::string department;

public:

// Constructor

Manager(const std::string& name, const std::string& department)

: Employee(name), department(department) {}

// Overriding the display function to also display manager's


department

void display() override {

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

std::cout << "Department: " << department << std::endl;

};

int main() {

// Creating an Employee object

Employee emp("John Doe");

// Displaying employee's name

emp.display();
// Creating a Manager object

Manager mgr("Jane Smith", "Sales");

// Displaying manager's name and department

mgr.display();

return 0;

OUTPUT:

QUESTION 3:
CODE:
#include <iostream>

// Base class Student

class Student {

protected:

double gpa;

public:

// Constructor

Student(double gpa) : gpa(gpa) {}

// Virtual function to calculate GPA

virtual double calculateGPA() {

return gpa;

};

// Derived class GraduateStudent

class GraduateStudent : public Student {

public:
// Constructor

GraduateStudent(double gpa) : Student(gpa) {}

// Overriding the calculateGPA function to use a different GPA


calculation formula

double calculateGPA() override {

// Example: For graduate students, GPA calculation formula might


be different

// For demonstration purposes, let's say we just add 0.5 to the GPA

return gpa + 0.5;

};

int main() {

// Creating a Student object

Student std(3.8);

// Calculating and displaying student's GPA

std::cout << "Student's GPA: " << std.calculateGPA() << std::endl;

// Creating a GraduateStudent object


GraduateStudent gradStd(3.5);

// Calculating and displaying graduate student's GPA

std::cout << "Graduate Student's GPA: " << gradStd.calculateGPA() <<


std::endl;

return 0;

OUTPUT:

QUESTION 4:

CODE:
#include <iostream>
// Abstract base class Account

class Account {

public:

// Pure virtual function to calculate interest

virtual double calculateInterest() = 0;

};

// Derived class SavingsAccount

class SavingsAccount : public Account {

private:

double balance;

double interestRate;

public:

// Constructor

SavingsAccount(double balance, double interestRate) :


balance(balance), interestRate(interestRate) {}

// Implementation of calculateInterest for SavingsAccount


double calculateInterest() override {

return balance * interestRate;

};

// Derived class CheckingAccount

class CheckingAccount : public Account {

private:

double balance;

double transactionFee;

public:

// Constructor

CheckingAccount(double balance, double transactionFee) :


balance(balance), transactionFee(transactionFee) {}

// Implementation of calculateInterest for CheckingAccount

double calculateInterest() override {

// Checking account usually does not earn interest, so returning 0


here
return 0.0;

};

int main() {

// Example usage

SavingsAccount savings(1000, 0.05);

std::cout << "Interest earned on savings account: $" <<


savings.calculateInterest() << std::endl;

CheckingAccount checking(2000, 2.0);

std::cout << "Interest earned on checking account: $" <<


checking.calculateInterest() << std::endl;

return 0;

OUTPUT:
CONCLUSION:
In Lab 101 , we learnt about inheritance of the class in which there is
one main base class while other classes are derived from it and public
data members of base class can be used in derived class .Also we
learnt about polymorphism and created class pointers and array.

You might also like