0% found this document useful (0 votes)
5 views22 pages

Oops Lab

The document contains a list of programming exercises that cover various concepts in C++, including classes, constructors, inheritance, exception handling, and templates. Each exercise includes a brief description, code implementation, and expected output. The exercises are designed to help learners understand fundamental programming principles and practices.

Uploaded by

fone24757
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)
5 views22 pages

Oops Lab

The document contains a list of programming exercises that cover various concepts in C++, including classes, constructors, inheritance, exception handling, and templates. Each exercise includes a brief description, code implementation, and expected output. The exercises are designed to help learners understand fundamental programming principles and practices.

Uploaded by

fone24757
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/ 22

INDEX

Sr.N Program Name Page Date Teacher


o. No. Signature

1. Write a program to create a simple class


and objects.

2. Write a program to check whether


number is even or odd.

3. Write a program to calculate factorial


using recursion.

4. Find the sum of first n Natural Numbers.

5. Write a program to demonstrate the


concept of constructor.

6. Write a program to demonstrate the


concept of static member function.

7. Write a program to demonstrate the


concept of friend function.

8. Write a program to demonstrate the


concept the concept of constructor
overloading.

9. Write a program to demonstrate the


concept of single inheritance.

10. Write a program to demonstrate the


concept of multiple inheritance.

11. Write a program to demonstrate the


concept of exception handling.

12. Write a program to demonstrate the


concept of templates.

1. Write a program to create a simple class and objects.


#include<iostream.h>
#include<conio.h>
Class hello
{
Public:
sayhello()
{
cout<<”Hello World!;
}
};
main()
{
clrscr();
Hello h;
h.sayhello();
getch();
}

Output:

Hello World !

2. Write a program to check whether number is even or odd.


#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter an integer: ";


cin >> n;

if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";

return 0;
}

Output:
Enter an integer: 23
23 is odd.

3. Write a program to calculate factorial using recursion.


#include<iostream>

using namespace std;

int factorial(int n);

int main() {

int n;

cout << "Enter a positive integer: ";

cin >> n;

cout << "Factorial of " << n << " = " << factorial(n);

return 0;

int factorial(int n) {

if(n > 1)

return n * factorial(n - 1);

else

return 1;

Output:

Enter an positive integer: 6

Factorial of 6 = 720

4.Find the sum of first n Natural Numbers.


#include <iostream>

using namespace std;

int main() {

int num, sum;

sum = 0;

cout << "Enter a positive integer: ";

cin >> num;

for (int i = 1; i <= num; ++i) {

sum += i;

cout << "Sum = " << sum << endl;

return 0;

Output:
Enter a positive integer: 10

Sum = 55

5.Write a program to demonstrate the concept of constructor.


#include <iostream>

using namespace std;

class Person{

// declaring private class data members

private:

string name;

int age;

public:

// declaring constructor

Person()

cout<<"Default constructor is called"<<endl;

name = "student";

age = 12;

// display function to print the class data members value

void display()

cout<<"Name of current object: "<<name<<endl;

cout<<"Age of current object: "<<age<<endl;

}
};

int main()

// creating object of class using default constructor

Person obj;

// printing class data members

obj.display();

return 0;

Output:

Default constructor is called

Name of current object: student

Age of current object: 12

6. Write a program to demonstrate the concept of static member


function.

#include <iostream>

using namespace std;


class MyClass {

public:

static void displayMessage() {

cout << "Hello, World!";

};

int main() {

// Calling static member function

MyClass::displayMessage();

return 0;

Output:

Hello, World!

7.Write a program to demonstrate the concept of friend function.

#include<iostream.h>

#include<conio.h>

class number
{

int a;

public:

get_num(int x);

friend print_num(number num);

};

number::get_num(int x)

a=x;

number::print_num(number num)

cout<<"Value of a :"<<num.a;

main()

clrscr();

number obj;

obj.get_num(1000);

print_num(obj);

getch();
}

Output:

Value of a : 1000

8.Write a program to demonstrate the concept the concept of


constructor overloading.

#include <iostream>

using namespace std;

class Rectangle {

public:
int length, width;

// Default constructor (no parameters)

Rectangle() {

length = 1;

width = 1;

// Constructor with one parameter (square)

Rectangle(int side) {

length = side;

width = side;

// Constructor with two parameters (rectangle)

Rectangle(int l, int w) {

length = l;

width = w;

// Method to display the area of the rectangle

void displayArea() {

cout << "Area: " << length * width << endl;

}
};

int main() {

// Using different constructors

Rectangle rect1; // Default constructor

Rectangle rect2(5); // Constructor with one parameter

Rectangle rect3(5, 3); // Constructor with two parameters

rect1.displayArea();

rect2.displayArea();

rect3.displayArea();

return 0;

Output:

Area: 1

Area: 25

Area: 15
9.Write a program to demonstrate the concept of single
inheritance.

#include <iostream>

using namespace std;

// Base class

class Shape {

public:

void setWidth(int w) {
width = w;

void setHeight(int h) {

height = h;

protected:

int width;

int height;

};

// Derived class

class Rectangle: public Shape {

public:

int getArea() {

return (width * height);

};

int main(void) {

Rectangle Rect;

Rect.setWidth(5);

Rect.setHeight(7);

// Print the area of the object.


cout << "Total area: " << Rect.getArea() << endl;

return 0;

Output:

Total area: 35

10.Write a program to demonstrate the concept of multiple


inheritance.

#include <iostream>

using namespace std;

// Base class 1

class Car {

public:

void drive() {

cout << "Driving on land" << endl;

};

// Base class 2

class Boat {

public:
void sail() {

cout << "Sailing on water" << endl;

};

// Derived class

class DualModeVehicle: public Car, public Boat {

public:

void use() {

drive(); // Calls the drive function from Car

sail(); // Calls the sail function from Boat

};

int main() {

DualModeVehicle myVehicle;

myVehicle.use(); // Demonstrates both functionalities

return 0;

Output:

Driving on land

Sailing on water
11.Write a program to demonstrate the concept of exception
handling.

#include <iostream>

using namespace std;

int main() {

double numerator, denominator, divide;

cout << "Enter numerator: ";

cin >> numerator;

cout << "Enter denominator: ";

cin >> denominator;


try {

// throw an exception if denominator is 0

if (denominator == 0)

throw 0;

// not executed if denominator is 0

divide = numerator / denominator;

cout << numerator << " / " << denominator << " = " << divide <<
endl;

catch (int num_exception) {

cout << "Error: Cannot divide by " << num_exception << endl;

return 0;

Output 1

Enter numerator: 72

Enter denominator: 0

Error: Cannot divide by 0

Output 2

Enter numerator: 72

Enter denominator: 3
72 / 3 = 24

12.Write a program to demonstrate the concept of templates.

#include <iostream>

#include <string>

using namespace std;

template <typename T>

inline T const& Max (T const& a, T const& b) {

return a < b ? b:a;

int main () {

int i = 39;
int j = 20;

cout << "Max(i, j): " << Max(i, j) << endl;

double f1 = 13.5;

double f2 = 20.7;

cout << "Max(f1, f2): " << Max(f1, f2) << endl;

string s1 = "Hello";

string s2 = "World";

cout << "Max(s1, s2): " << Max(s1, s2) << endl;

return 0;

Output:

Max(i, j): 39

Max(f1, f2): 20.7

Max(s1, s2): World

You might also like