0% found this document useful (0 votes)
37 views51 pages

Opp Assignment 101

This document is an assignment for an Object-Oriented Programming (OOP) course, detailing various tasks that demonstrate key OOP concepts in C++. It includes code examples for basic operations, class implementations, and problem-solving scenarios such as calculating averages, finding maximum numbers, and demonstrating inheritance. The assignment is submitted by Hasnaat Anjum with a focus on practical applications of OOP principles.
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)
37 views51 pages

Opp Assignment 101

This document is an assignment for an Object-Oriented Programming (OOP) course, detailing various tasks that demonstrate key OOP concepts in C++. It includes code examples for basic operations, class implementations, and problem-solving scenarios such as calculating averages, finding maximum numbers, and demonstrating inheritance. The assignment is submitted by Hasnaat Anjum with a focus on practical applications of OOP principles.
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/ 51

Object-Oriented Programming (OOP)Assignment

Course: Object-Oriented Programming (OOP)


Instructor: [Arsalan Ali Mansab]
Submitted by: [Hasnaat Anjum]
Roll Number: [E3-24-101]
Date: [05-05-2025]

Objective
This assignment demonstrates the implementation of key OOP concepts such as classes,
objects, inheritance, polymorphism, constructors, destructors, and encapsulation using C++.
The programs cover a variety of practical scenarios to reinforce understanding.

Task 1: Basic Class and Object


Description: A simple program to display a name using C++ I/O operations.
cpp
#include <iostream>
using namespace std;

int main() {
cout << "Hasnaat Anjum" << endl;
return 0;
}

Output:
Hasnaat Anjum
Task 2: Switch-Case for Day Mapping
Description: Map a number (1-7) to a weekday using switch-case.

cpp
#include <iostream>
using namespace std;

int main() {
int dayNumber;
cout << "Enter a number (1-7): ";
cin >> dayNumber;

switch (dayNumber) {
case 1: cout << "Monday" << endl; break;
case 2: cout << "Tuesday" << endl; break;
case 3: cout << "Wednesday" << endl; break;
case 4: cout << "Thursday" << endl; break;
case 5: cout << "Friday" << endl; break;
case 6: cout << "Saturday" << endl; break;
case 7: cout << "Sunday" << endl; break;
default: cout << "Invalid input!" << endl;
}
return 0;
}

Output:

Enter a number (1-7): 3


Wednesday
Task 3: Average Marks Calculation
Description: Calculate average marks and determine admission eligibility.

cpp
#include <iostream>
using namespace std;

int main() {
float marks[5], sum = 0, average;
cout << "Enter marks for 5 subjects: ";
for (int i = 0; i < 5; i++) {
cin >> marks[i];
sum += marks[i];
}
average = sum / 5;
cout << "Average marks: " << average << endl;

if (average >= 80 && average <= 100) {


cout << "Admission granted!" << endl;
} else {
cout << "Admission denied." << endl;
}
return 0;
}
Output:

Enter marks for 5 subjects: 85 90 88 92 95


Average marks: 90
Admission granted!
Task 4: Nested Loop Pattern
Description: Print a star pattern using nested loops.

cpp
#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}

Output:

*
**
***
****
*****

Task 5: Maximum Number Finder


Description: Find the maximum of three numbers using conditional statements.

cpp
#include <iostream>
using namespace std;

int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;

if (a > b && a > c) {


cout << a << " is the maximum." << endl;
} else if (b > a && b > c) {
cout << b << " is the maximum." << endl;
} else {
cout << c << " is the maximum." << endl;
}
return 0;
}

Output:

Enter three numbers: 5 9 2


9 is the maximum.

Task 6: Circle Area and Circumference


Description: Calculate area and circumference of a circle using a class.

cpp
#include <iostream>
using namespace std;

class Circle {
private:
float radius;
public:
void setRadius(float r) {
radius = r;
}
float getArea() {
return 3.14 * radius * radius;
}
float getCircumference() {
return 2 * 3.14 * radius;
}
};

int main() {
Circle c;
c.setRadius(7);
cout << "Area: " << c.getArea() << endl;
cout << "Circumference: " << c.getCircumference() << endl;
return 0;
}

Output:

Area: 153.86
Circumference: 43.96
Task 7: Grade System
Description: Assign grades based on marks using if-else ladder.

cpp
#include <iostream>
using namespace std;

int main() {
int marks;
cout << "Enter marks: ";
cin >> marks;

if (marks >= 90) {


cout << "Grade: A" << endl;
} else if (marks >= 80) {
cout << "Grade: B" << endl;
} else if (marks >= 70) {
cout << "Grade: C" << endl;
} else if (marks >= 60) {
cout << "Grade: D" << endl;
} else {
cout << "Grade: F" << endl;
}
return 0;
}

Output:
Enter marks: 85
Grade: B
Task 8: Diamond Problem Demonstration
Description: Resolve the diamond problem using virtual inheritance.

cpp
#include <iostream>
using namespace std;

class A {
public:
void display() {
cout << "Class A" << endl;
}
};

class B : virtual public A {};


class C : virtual public A {};
class D : public B, public C {};

int main() {
D obj;
obj.display(); // No ambiguity due to virtual inheritance
return 0;
}

Output:

Class A
Task 9: Method Overriding
Description: Override arithmetic operations in a derived class for input validation.

cpp
#include <iostream>
using namespace std;

class Simple {
protected:
int a, b;
public:
void input() {
cout << "Enter two numbers: ";
cin >> a >> b;
}
void add() {
cout << "Sum: " << a + b << endl;
}
};

class Advanced : public Simple {


public:
void add() {
if (a <= 0 || b <= 0) {
cout << "Invalid values!" << endl;
} else {
Simple::add();
}
}
};

int main() {
Advanced obj;
obj.input();
obj.add();
return 0;
}

Output:

Enter two numbers: 5 0


Invalid values!

Task 10: Calculator Class


Description: Build a calculator with basic operations using a class.

cpp
#include <iostream>
using namespace std;

class Calculator {
private:
float a, b;
public:
void input() {
cout << "Enter two numbers: ";
cin >> a >> b;
}
float add() { return a + b; }
float subtract() { return a - b; }
float multiply() { return a * b; }
float divide() {
if (b == 0) {
cout << "Error: Division by zero!" << endl;
return 0;
}
return a / b;
}
};

int main() {
Calculator calc;
calc.input();
cout << "Sum: " << calc.add() << endl;
cout << "Difference: " << calc.subtract() << endl;
cout << "Product: " << calc.multiply() << endl;
cout << "Quotient: " << calc.divide() << endl;
return 0;
}

Output:
Enter two numbers: 10 2
Sum: 12
Difference: 8
Product: 20
Quotient: 5
Task 11: Book Price Comparison
Description: Compare prices of two books and display the costlier one.

cpp
#include <iostream>
using namespace std;

class Book {
private:
string title;
float price;
public:
void input() {
cout << "Enter book title: ";
cin >> title;
cout << "Enter price: ";
cin >> price;
}
void display() {
cout << "Book: " << title << ", Price: " << price << endl;
}
bool isCostlier(Book b) {
return price > b.price;
}
};

int main() {
Book b1, b2;
b1.input();
b2.input();

if (b1.isCostlier(b2)) {
cout << "Costlier book: ";
b1.display();
} else {
cout << "Costlier book: ";
b2.display();
}
return 0;
}

Output:

Enter book title: OOP


Enter price: 500
Enter book title: DSA
Enter price: 600
Costlier book: Book: DSA, Price: 600

Task 12: Leap Year Checker


Description: Check if a year is a leap year using logical conditions.

cpp
#include <iostream>
using namespace std;

int main() {
int year;
cout << "Enter a year: ";
cin >> year;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


cout << year << " is a leap year." << endl;
} else {
cout << year << " is not a leap year." << endl;
}
return 0;
}

Output:

Enter a year: 2024


2024 is a leap year.

Task 13: Position Tracking with Inheritance


Description: Track movement (forward/backward) using class Movement and derived class
Move.

cpp
#include <iostream>
using namespace std;

class Movement {
protected:
int pos;
public:
Movement() : pos(0) {}
void showPosition() {
cout << "Current position: " << pos << endl;
}
};

class Move : public Movement {


public:
void forward(int steps) {
pos += steps;
}
void backward(int steps) {
pos -= steps;
}
};

int main() {
Move obj;
obj.forward(5);
obj.showPosition();
obj.backward(2);
obj.showPosition();
return 0;
}

Output:
Current position: 5
Current position: 3
Task 4: Constructors and Destructor
Description: Demonstrate constructor overloading and destructor invocation.

cpp
#include <iostream>
using namespace std;

class Test {
public:
Test() {
cout << "Constructor called." << endl;
}
~Test() {
cout << "Destructor called." << endl;
}
};

int main() {
Test obj;
return 0;
}

Output:

Constructor called.
Destructor called.
1.Basic Arithmetic Operations (arithmetic.cxx)
Description: Performs addition, subtraction, multiplication, and division on two numbers.

Code:
cpp
#include <iostream>
using namespace std;

void arithmetic(int a, int b) {


int sum = a + b;
cout << a << " + " << b << " = " << sum << endl;

int subtract = a - b;
cout << a << " - " << b << " = " << subtract << endl;

int product = a * b;
cout << a << " * " << b << " = " << product << endl;

if (b != 0) {
int division = a / b;
cout << a << " / " << b << " = " << division << endl;
} else {
cout << "Division by zero is not possible" << endl;
}
}

int main() {
int a, b;
cout << "Enter first number: ";
cin >> a;

cout << "Enter second number: ";


cin >> b;

cout << "\nResults:" << endl;


arithmetic(a, b);

return 0;
}

Output:

Enter first number: 10


Enter second number: 5

Results:
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2

2.Name Class (function.cxx)


Description: A simple class to input and display a name.

Code:
cpp
#include <iostream>
using namespace std;

class Name {
private:
string name;

public:
void inputName() {
cout << "Enter name: ";
getline(cin, name);
}

void displayName() {
cout << "Your name is: " << name << endl;
}
};

int main() {
Name obj;
obj.inputName();
obj.displayName();
return 0;
}

Output:
Enter name: Hasnaat
Your name is: Hasnaat
3. Matrix Operations (user matrix.cxx)

Description: Calculates sum of diagonal elements and product of non-diagonal elements in a


3x3 matrix.

Code:
cpp
#include <iostream>
using namespace std;

int main() {
int arr[3][3], diagonalSum = 0;
long long productOther = 1;

cout << "Enter elements of the 3x3 matrix:\n";


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
cin >> arr[i][j];
if (i != j) productOther *= arr[i][j];
else diagonalSum += arr[i][j];
}

cout << "Product of other elements: " << productOther << endl;
cout << "Sum of diagonal elements: " << diagonalSum << endl;

return 0;
}

*Output*:
Enter elements of the 3x3 matrix:
123
456
789
Product of other elements: 362880
Sum of diagonal elements: 15

---

4. Array Operations (array1.cxx)


Description: Calculates sum of even numbers and product of odd numbers in an array.

Code:
cpp
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;

int arr[n];
int evenSum = 0, oddProduct = 1;

cout << "Enter " << n << " numbers:\n";


for(int i = 0; i < n; i++) {
cin >> arr[i];
if(arr[i] % 2 == 0) {
evenSum += arr[i];
} else {
oddProduct *= arr[i];
}
}
cout << "Even sum: " << evenSum << "\nOdd product: " << oddProduct;
return 0;
}

*Output*:

Enter number of elements: 5


Enter 5 numbers:
12345
Even sum: 6
Odd product: 15

---

5. Book Class (Version 1) (book.cxx)


Description: Stores book details and compares prices (initial version with issues).

Code:
cpp
#include <iostream>
#include <string>
using namespace std;

class Book {
public:
Book(int i, string n, string a, int p) : id(i), name(n), author(a), price(p) {}
int id;
string name, author;
int price;
};

int main() {
int id1, id2, price1, price2;
string name1, name2, author1, author2;

cout << "Book 1 (ID Name Author Price): ";


cin >> id1;
cin.ignore();
getline(cin, name1);
getline(cin, author1);
cin >> price1;

cout << "Book 2 (ID Name Author Price): ";


cin >> id2;
cin.ignore();
getline(cin, name2);
getline(cin, author2);
cin >> price2;

Book b1(id1, name1, author1, price1);


Book b2(id2, name2, author2, price2);

cout << "\nHigher priced book: " << (b1.price > b2.price ? b1.name : b2.name);
return 0;
}

*Output*:

Book 1 (ID Name Author Price): 101 The_Catcher_in_the_Rye J.D._Salinger 200


Book 2 (ID Name Author Price): 102 To_Kill_a_Mockingbird Harper_Lee 250

Higher priced book: To_Kill_a_Mockingbird

---

6. Book Class (Version 2) (book2.cxx)


Description: Improved book class with member functions.

Code:
cpp
#include<iostream>
using namespace std;

class book {
public:
int id;
string name;
string author;
int price;

void get() {
cout << "Enter book id: ";
cin >> id;
cout << "Enter book name: ";
cin.ignore();
getline(cin, name);
cout << "Enter book author: ";
getline(cin, author);
cout << "Enter book price: ";
cin >> price;
}

void show() {
cout << "Book id: " << id << endl;
cout << "Book name: " << name << endl;
cout << "Book author: " << author << endl;
cout << "Book price: " << price << endl;
}
};

int main() {
book b1, b2;
cout << "Enter details about book 1:\n";
b1.get();
cout << "Enter details about book 2:\n";
b2.get();
if (b1.price > b2.price) {
b1.show();
} else if (b2.price > b1.price) {
b2.show();
}
return 0;
}

*Output*:

Enter details about book 1:


Enter book id: 101
Enter book name: 1984
Enter book author: George Orwell
Enter book price: 300
Enter details about book 2:
Enter book id: 102
Enter book name: Brave New World
Enter book author: Aldous Huxley
Enter book price: 350
Book id: 102
Book name: Brave New World
Book author: Aldous Huxley
Book price: 350

---
7. Array Class with Max/Min (max.cxx)
Descriptio: Class to find maximum and minimum values in an array.

Code:
cpp
#include <iostream>
#include <climits>
using namespace std;

class Array {
private:
int arr[5];

public:
void fill() {
cout << "Enter 5 numbers: \n";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
}

void display() {
cout << "Array elements: ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int max() {
int maxVal = INT_MIN;
for (int i = 0; i < 5; i++) {
if (arr[i] > maxVal) maxVal = arr[i];
}
return maxVal;
}

int min() {
int minVal = INT_MAX;
for (int i = 0; i < 5; i++) {
if (arr[i] < minVal) minVal = arr[i];
}
return minVal;
}
};

int main() {
Array arrObj;
arrObj.fill();
arrObj.display();
cout << "Maximum value: " << arrObj.max() << endl;
cout << "Minimum value: " << arrObj.min() << endl;
return 0;
}

*Output*:
Enter 5 numbers:
37194
Array elements: 3 7 1 9 4
Maximum value: 9
Minimum value: 1

---

8. Day of Week (day.cxx)


Description: Displays day name based on number input.

Code:
cpp
#include <iostream>
using namespace std;

int main() {
int dayNumber;
cout << "Enter day number (1-7): ";
cin >> dayNumber;

switch (dayNumber) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
case 3: cout << "Wednesday"; break;
case 4: cout << "Thursday"; break;
case 5: cout << "Friday"; break;
case 6: cout << "Saturday"; break;
case 7: cout << "Sunday"; break;
default: cout << "Invalid input!";
}
return 0;
}

*Output*:

Enter day number (1-7): 3


Wednesday

---

9. Arithmetic opreations

metic Class (class.cxx)


Description: Class for basic arithmetic operations.

Code:
cpp
#include <iostream>
using namespace std;

class Arithmetic {
private:
int a, b;

public:
void inputNumbers() {
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
}

int add() { return a + b; }


int subtract() { return a - b; }
int multiply() { return a * b; }
int divide() {
if (b != 0) return a / b;
cout << "Error! Division by zero." << endl;
return 0;
}
};

int main() {
Arithmetic obj;
obj.inputNumbers();
cout << "Addition: " << obj.add() << endl;
cout << "Subtraction: " << obj.subtract() << endl;
cout << "Multiplication: " << obj.multiply() << endl;
cout << "Division: " << obj.divide() << endl;
return 0;
}

*Output*:
Enter first number: 10
Enter second number: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2

---

10. Inheritance with Method Overriding (overriding inheritance.cxx)


Description : Demonstrates method overriding in inheritance.

Code:
cpp
#include <iostream>
using namespace std;

class Simple {
protected:
int a, b;

public:
Simple() { a = b = 0; }

void in() {
cout << "Enter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;
}

void add() { cout << "a + b = " << a + b << endl; }


void sub() { cout << "a - b = " << a - b << endl; }
void mul() { cout << "a * b = " << a * b << endl; }
void div() { cout << "a / b = " << a / b << endl; }
};

class Complex : public Simple {


public:
void add() {
if (a <= 0 || b <= 0) cout << "Invalid values." << endl;
else Simple::add();
}

void sub() {
if (a <= 0 || b <= 0) cout << "Invalid values." << endl;
else Simple::sub();
}

void mul() {
if (a <= 0 || b <= 0) cout << "Invalid values." << endl;
else Simple::mul();
}

void div() {
if (a <= 0 || b <= 0) cout << "Invalid values." << endl;
else Simple::div();
}
};

int main() {
Complex obj;
obj.in();
obj.add();
obj.sub();
obj.mul();
obj.div();
return 0;
}

*Output*:

Enter a: 10
Enter b: 5
a + b = 15
a-b=5
a * b = 50
a/b=2

---

11. Diamond Inheritance (diamond.cxx)


Description: Demonstrates diamond problem in inheritance.
*Code*:
cpp
#include <iostream>
using namespace std;

class A {
public:
void show() { cout << "Class A\n"; }
};

class B : public A {};


class C : public A {};
class D : public B, public C {};

int main() {
D obj;
obj.B::show();
obj.C::show();
return 0;
}

*Output*:

Class A
Class A
---

12. Polymorphism Example (polymorphism.cxx)


Description: Demonstrates polymorphism with object counting.

Code:
cpp
#include <iostream>
using namespace std;

int total = 0;

class Vehicle {
private:
int id;

public:
Vehicle(int initid) {
id = initid;
total++;
}

~Vehicle() {
total--;
}
};

int main() {
Vehicle a(22), b(33), c(99), x(44), y(33);
cout << "Count of vehicles: " << total << endl;

Vehicle *xp;
{
Vehicle v(55);
xp = new Vehicle(77);
cout << "Count of vehicles: " << total << endl;
}

delete xp;
cout << "Count of vehicles: " << total << endl;

return 0;
}

*Output*:

Count of vehicles: 5
Count of vehicles: 7
Count of vehicles: 4

---

13. Book Class with Pointers (pointer book.cxx)


Description: Uses pointers to manage book objects.

Code:
cpp
#include<iostream>
using namespace std;

class book {
public:
int id;
string name;
string author;
int price;

void get() {
cout << "Enter book id: ";
cin >> id;
cout << "Enter book name: ";
cin.ignore();
getline(cin, name);
cout << "Enter book author: ";
getline(cin, author);
cout << "Enter book price: ";
cin >> price;
}

void show() {
cout << "\nBook ID: " << id << endl;
cout << "Book Name: " << name << endl;
cout << "Book Author: " << author << endl;
cout << "Book Price: " << price << endl;
}
};

int main() {
book *b1 = new book;
book *b2 = new book;

cout << "Enter details about Book 1:\n";


b1->get();

cout << "\nEnter details about Book 2:\n";


b2->get();

cout << "\nBook with higher price:\n";


if (b1->price > b2->price) {
b1->show();
} else if (b2->price > b1->price) {
b2->show();
} else {
cout << "Both books have the same price." << endl;
}

delete b1;
delete b2;
return 0;
}

*Output*:
Enter details about Book 1:
Enter book id: 101
Enter book name: The Hobbit
Enter book author: J.R.R. Tolkien
Enter book price: 300

Enter details about Book 2:


Enter book id: 102
Enter book name: The Lord of the Rings
Enter book author: J.R.R. Tolkien
Enter book price: 400

Book with higher price:


Book ID: 102
Book Name: The Lord of the Rings
Book Author: J.R.R. Tolkien
Book Price: 400

---

14. Arithmetic with Pointers (arthimetic pointer.cxx)


Description: Uses pointers for arithmetic operations.

Code:
cpp
#include <iostream>
using namespace std;
class Arithmetic {
private:
int a, b;

public:
void inputNumbers() {
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
}

int add() { return a + b; }


int subtract() { return a - b; }
int multiply() { return a * b; }
int divide() {
if (b != 0) return a / b;
cout << "Error! Division by zero." << endl;
return 0;
}
};

int main() {
Arithmetic *obj = new Arithmetic;
obj->inputNumbers();

cout << "Addition: " << obj->add() << endl;


cout << "Subtraction: " << obj->subtract() << endl;
cout << "Multiplication: " << obj->multiply() << endl;
cout << "Division: " << obj->divide() << endl;

delete obj;
return 0;
}

*Output*;

Enter first number: 20


Enter second number: 5
Addition: 25
Subtraction: 15
Multiplication: 100
Division: 4

---

15. Constructors and Destructors (destructer.cxx)


Description: Demonstrates constructor overloading and destructor.

*Code*:
cpp
#include<iostream>
using namespace std;

class test {
public:
test() { cout << "Default constructor" << endl; }
test(int a) { cout << "Number is: " << a << endl; }
test(int a, int b) { cout << "Sum is: " << a+b << endl; }
~test() { cout << "Destructor called" << endl; }
};

int main() {
test t, t2(0), t3(5, 10);
return 0;
}

*Output*:

Default constructor
Number is: 0
Sum is: 15
Destructor called
Destructor called
Destructor called

---

16. Multiple Inheritance (multiplye inheritance.cxx)


Description : Demonstrates multiple inheritance with constructor calls.

Code:
cpp
#include <iostream>
using namespace std;

class A {
public:
A() { cout << "Constructor of class A" << endl; }
};

class B {
public:
B() { cout << "Constructor of class B" << endl; }
};

class C : public A, public B {


public:
C() : B(), A() { cout << "Constructor of class C" << endl; }
};

int main() {
C obj;
return 0;
}

Output:

Constructor of class A
Constructor of class B
Constructor of class C
---

17. Virtual Functions (early binding.cxx)


Description : Demonstrates runtime polymorphism using virtual functions.

Code:
cpp
#include <iostream>
using namespace std;

class Parent {
public:
virtual void show() { cout << "Parent class" << endl; }
};

class Child1 : public Parent {


public:
void show() { cout << "Child1 class" << endl; }
};

class Child2 : public Parent {


public:
void show() { cout << "Child2 class" << endl; }
};

int main() {
Parent* ptr[5];
int op, i;

cout << "Enter 1 for Parent, 2 for Child1, 3 for Child2" << endl;
for (i = 0; i < 5; i++) {
cout << "Which object to create? ";
cin >> op;

if (op == 1) ptr[i] = new Parent;


else if (op == 2) ptr[i] = new Child1;
else ptr[i] = new Child2;
}

for (i = 0; i < 5; i++) {


ptr[i]->show();
}

return 0;
}

Output:

Enter 1 for Parent, 2 for Child1, 3 for Child2


Which object to create? 1
Which object to create? 2
Which object to create? 3
Which object to create? 2
Which object to create? 1
Parent class
Child1 class
Child2 class
Child1 class
Parent class

---

18. Movement Class (move inher.cxx)


Description: Demonstrates inheritance with movement tracking.

Code:
cpp
#include <iostream>
using namespace std;

class Movement {
protected:
int pos;

public:
Movement() { pos = 0; }
void show() { cout << "Position = " << pos << endl; }
};

class Move : public Movement {


public:
void forward(int steps) { pos += steps; }
void backward(int steps) { pos -= steps; }
};

int main() {
Move m;
m.show();
m.forward(4);
m.show();
m.backward(1);
m.show();
return 0;
}

Output:

Position = 0
Position = 4
Position = 3

---

19. Multi-level Inheritance (class inheritance.cxx)


Description: Demonstrates multi-level inheritance.

Code:
cpp
#include <iostream>
using namespace std;
class A {
private:
int a;
public:
void in() {
cout << "Enter a: ";
cin >> a;
}
void out() {
cout << "a = " << a << endl;
}
};

class B : public A {
private:
int b;
public:
void in() {
A::in();
cout << "Enter b: ";
cin >> b;
}
void out() {
A::out();
cout << "b = " << b << endl;
}
};
class C : public B {
private:
int c;
public:
void in() {
B::in();
cout << "Enter c: ";
cin >> c;
}
void out() {
B::out();
cout << "c = " << c << endl;
}
};

int main() {
C obj;
obj.in();
obj.out();
return 0;
}

Output:

Enter a: 10
Enter b: 20
Enter c: 30
a = 10
b = 20
c = 30

You might also like