Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

1. (a) Write a program in C++ for Function Overloading.

#include<iostream>
#include<cstdlib>
using namespace std;

float area(float r)
{
return(3.14 * r * r);
}
int area(int s)
{
return(s * s);
}
float area(float l,float b)
{
return (l * b);
}
int main()
{
float b,r,l;
int s,ch;

do
{
cout<<"\n\n *****Menu***** \n";
cout<<"\n 1. Area of Circle";
cout<<"\n 2. Area of Square";
cout<<"\n 3. Area of Rectangle";
cout<<"\n 4. Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\n Enter the Radius of Circle : ";
cin>>r;
cout<<"\n Area of Circle : "<<area(r);
break;
}
case 2:
{
cout<<"\n Enter the side of Square: ";
cin>>s;
cout<<"\n Area of Square: "<<area(s);
break;
}
case 3:
{
cout<<"\n Enter the Length & Breadth of Rectangle : ";
cin>>l>>b;
cout<<"\n Area of Rectangle : "<<area(l,b);
break;
}
case 4:
exit(0);
default:
cout<<"\n Invalid Choice... ";
}
}while(ch!=4);
return 0;
}

1(b) Write a C++ program to implement inline function and default arguments.

#include <iostream>
using namespace std;
// Inline function with default arguments
inline int addNumbers(int a, int b = 0) {
return a + b;
}

int main() {
int num1 = 5;
int num2 = 3;

int result1 = addNumbers(num1, num2); // Calls the function with both arguments
int result2 = addNumbers(num1); // Calls the function with only one argument

cout << "Result 1: " << result1 << endl;


cout << "Result 2: " << result2 << endl;

return 0;
}
1(c) Write a program in C++ for call by reference and return by reference.

#include <iostream>
using namespace std;

// Function to swap two numbers using call by reference


void swapNumbers(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}

// Function to increment a number by 1 using return by reference


int& incrementByOne(int& num) {
num += 1;
return num;
}

int main() {
int num1 = 5;
int num2 = 10;

cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;

// Swapping numbers using call by reference


swapNumbers(num1, num2);

cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;

int num3 = 7;

cout << "Before increment: num3 = " << num3 << endl;

// Incrementing number by 1 using return by reference


int& incrementedNum = incrementByOne(num3);

cout << "After increment: num3 = " << incrementedNum << endl;

return 0;
}

2(a) Write a program in C++ where you use different data types as data members of class.
#include <iostream>
#include <string>
using namespace std;

class DataTypes {
public:
int intValue;
double doubleValue;
char charValue;
string stringValue;

// Function to set integer value


void setIntValue(int value) {
intValue = value;
}

// Function to set double value


void setDoubleValue(double value) {
doubleValue = value;
}

// Function to set char value


void setCharValue(char value) {
charValue = value;
}

// Function to set string value


void setStringValue(const string& value) {
stringValue = value;
}

// Function to display the values of the data members


void displayValues() {
cout << "Integer Value: " << intValue << endl;
cout << "Double Value: " << doubleValue << endl;
cout << "Char Value: " << charValue <<endl;
cout << "String Value: " << stringValue << endl;
}
};

int main() {
DataTypes data;

data.setIntValue(42);
data.setDoubleValue(3.14);
data.setCharValue('A');
data.setStringValue("Good Morning");

data.displayValues();

return 0;
}

2(b) Write a C++ program using the object as argument concept in OOP

#include <iostream>
using namespace std;

class Time {
private:
int hours;
int minutes;

public:
// Function to set the time in hours and minutes
void setTime(int h, int m) {
hours = h;
minutes = m;
}

// Function to display the time


void displayTime() {
cout << "Time: " << hours << " hours and " << minutes << " minutes" << endl;
}

// Function to add two Time objects and return the result


Time addTime(Time t) {
Time result;
result.minutes = minutes + t.minutes;
result.hours = hours + t.hours + (result.minutes / 60);
result.minutes %= 60;
return result;
}
};

int main() {
Time time1, time2, sum;
// Set time for time1 and display it
time1.setTime(2, 45);
cout << "Time 1:" << endl;
time1.displayTime();

// Set time for time2 and display it


time2.setTime(1, 30);
cout << "\nTime 2:" << endl;
time2.displayTime();

// Add two Time objects using the object as an argument concept


sum = time1.addTime(time2);

// Display the sum of times


cout << "\nSum of times:" << endl;
sum.displayTime();

return 0;
}

3(a) Write a C++ program to illustrate the Default Constructor.

Example:
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student() // Explicit Default constructor
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}

void display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
};

int main()
{
student s;
s.display();
return 0;
}

3(b) Write a C++ program to illustrate the Parameterized Constructor.

#include<iostream>
#include<string.h>
using namespace std;

class student
{
int rno;
char name[50];
double fee;

public:
student(int,char[],double);
void display();

};

student::student(int no,char n[],double f)


{
rno=no;
strcpy(name,n);
fee=f;
}

void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}

int main()
{
student s(1001,"Ram",10000);
s.display();
return 0;
}

3(c) Write a C++ program to illustrate the Copy Constructor.

#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
student(student &t) //copy constructor
{
rno=t.rno;
strcpy(name,t.name);
fee=t.fee;
}
void display();

};

student::student(int no,char n[],double f)


{
rno=no;
strcpy(name,n);
fee=f;
}

void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}

int main()
{
student s(1001,"Manjeet",10000);
student s1(1002,"Sana",50000);
s.display();
s1.display();

student manjeet(s);//copy constructor called


student sana(s1);
manjeet.display();
sana.display();

return 0;
}

4(a)

#include<iostream>
#include<string.h>
using namespace std;
class student
{
char sname[50];
float marks[6];
float total;
float max_marks;
public:
student();
void assign();
void compute();
void display();
};
student::student()
{
strcpy(sname," ");
for(int i=0;i<6;i++)
marks[i]=0;
total=0;
max_marks=0; }
void student::assign() {
cout<<endl<<"Enter Student Name :";
cin>>sname;
for(int i=0;i<6;i++)
{
cout<<"Enter marks of"<<i+1<<" subject:";
cin>>marks[i]; }
cout<<"Enter Maximum total marks";
cin>>max_marks;
}
void student::compute()
{
total=0;
for(int i=0;i<6;i++)
total+=marks[i];
}
void student::display()
{
cout<<"Student Name:"<<sname<<endl; cout<<"Marks are\n";
for(int i=0;i<6;i++)
cout<<"Subject "<<i+1<<": "<<marks[i]<<endl;
cout<<" -------------------\n";
cout<<"Total :"<<total<<endl;
cout<<" -------------------\n";
float per;
per=(total/max_marks)*100;
cout<<"Percentage:"<<per; }
int main() {
student obj;
obj.assign();
obj.compute();
obj.display();
return 0;

4(b)Write a Program to demonstrate friend function and friend class.


#include<iostream>
using namespace std;
class sample2;
class sample1
{
int x;
public:
sample1(int a);
friend void max(sample1 s1,sample2 s2);

};
sample1::sample1(int a)
{
x=a;
}
class sample2
{
int y;
public:
sample2(int b);
friend void max(sample1 s1,sample2 s2);
};
sample2::sample2(int b)
{
y=b;
}
void max(sample1 s1,sample2 s2)
{
if(s1.x>s2.y)
cout<<"Data member in Object of class sample1 is larger"<<endl;
else
cout<<"Data member in Object of class sample2 is larger "<<endl;
}
int main()
{
sample1 obj1(3);
sample2 obj2(5);
max(obj1,obj2);
return 0;
}
5(a)Write a C++ Program for Unary Operator Overloading using increment and decrement
operators?

#include<iostream>
using namespace std;

class complex {
int a, b, c;
public:

complex() {
}

void getvalue() {
cout << "Enter the Two Numbers:";
cin >> a>>b;
}

void operator++() {
a = ++a;
b = ++b;
}

void operator--() {
a = --a;
b = --b;
}

void display() {
cout << a << "+\t" << b << "i" << endl;
}
};

int main() {
complex obj;
obj.getvalue();
++obj;
cout << "Increment Complex Number\n";
obj.display();
--obj;
cout << "Decrement Complex Number\n";
obj.display();
return 0;
}
5(b) Write a C++ program to perform the arithmetic operation by overloading the multiple
binary operators

#include <iostream>
using namespace std;
class Arith_num
{
// declare data member or variable
int num;
public:
// create a member function to take input
void input()
{
num = 20; //define value to num variable
}
// use binary '+' operator to add number
Arith_num operator + (Arith_num &ob)
{
// create an object
Arith_num A;
// assign values to object
A.num = num + ob.num;
return (A);
}
// overload the binary (-) operator
Arith_num operator - (Arith_num &ob)
{
// create an object
Arith_num A;
// assign values to object
A.num = num - ob.num;
return (A);
}
// overload the binary (*) operator
Arith_num operator * (Arith_num &ob)
{
// create an object
Arith_num A;
// assign values to object
A.num = num * ob.num;
return (A);
}
// overload the binary (/) operator
Arith_num operator / (Arith_num &ob)
{
// create an object
Arith_num A;
// assign values to object
A.num = num / ob.num;
return (A);
}
// display the result of arithmetic operators
void print()
{
cout << num;
}
};
int main ()
{
Arith_num x1, y1, res; // here we created object of class Addition i.e x1 and y1
// accepting the values
x1.input();
y1.input();
// assign result of x1 and x2 to res
res = x1 + y1;
cout << " Addition : " ;
res.print();
// assign the results of subtraction to res
res = x1 - y1; // subtract the complex number
cout << " \n \n Subtraction : " ;
res.print();
// assign the multiplication result to res
res = x1 * y1;
cout << " \n \n Multiplication : " ;
res.print();
// assign the division results to res
res = x1 / y1;
cout << " \n \n Division : " ;
res.print();
return 0;
}

5(c) Write a C++ program to perform Operator Overloading Using Friend Function

#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int img;
public:
Complex (int r = 0, int i = 0)
{
real = r;
img = i;
}
void Display ()
{
cout << real << "+i" << img;
}
friend Complex operator + (Complex c1, Complex c2);
};

Complex operator + (Complex c1, Complex c2)


{
Complex temp;
temp.real = c1.real + c2.real;
temp.img = c1.img + c2.img;
return temp;
}

int main ()
{
Complex C1(5, 3), C2(10, 5), C3;
C1.Display();
cout << " + ";
C2.Display();
cout << " = ";
C3 = C1 + C2;
C3.Display();
}

6(a) Write a program to access the static data members in the C++ programming
language.

#include <iostream>
#include <string.h>
using namespace std;
// create class of the Car
class Car
{
private:
int car_id;
char car_name[20];
int marks;

public:
// declare a static data member
static int static_member;

Car()
{
static_member++;
}

void inp()
{
cout << " \n\n Enter the Id of the Car: " << endl;
cin >> car_id; // input the id
cout << " Enter the name of the Car: " << endl;
cin >> car_name;
cout << " Number of the Marks (1 - 10): " << endl;
cin >> marks;
}

// display the entered details


void disp ()
{
cout << " \n Id of the Car: " << car_id;
cout << "\n Name of the Car: " << car_name;
cout << " \n Marks: " << marks;

}
};

// initialized the static data member to 0


int Car::static_member = 0;

int main ()
{
// create object for the class Car
Car c1;
// call inp() function to insert values
c1. inp ();
c1. disp();

//create another object


Car c2;
// call inp() function to insert values
c2. inp ();
c2. disp();

cout << " \n No. of objects created in the class: " << Car :: static_member <<endl;
return 0;
}

6(b)Write a program to access the static member function using the object and class in
the C++ programming language.

#include <iostream>
using namespace std;
class Member
{

private:
// declaration of the static data members
static int A;
static int B;
static int C;

// declare public access specifier


public:
// define the static member function
static void disp ()
{
cout << " The value of the A is: " << A << endl;
cout << " The value of the B is: " << B << endl;
cout << " The value of the C is: " << C << endl;
}
};
// initialization of the static data members
int Member :: A = 20;
int Member :: B = 30;
int Member :: C = 40;

int main ()
{
// create object of the class Member
Member mb;
// access the static member function using the class object name
cout << " Print the static member through object name: " << endl;
mb. disp();
// access the static member function using the class name
cout << " Print the static member through the class name: " << endl;
Member::disp();
return 0;
}

7(a) Write a cpp program to convert basic datatype to user defined datatype.

#include <iostream>
using namespace std;
class CustomType {
private:
double value;

public:
// Constructor that takes an int and converts it to CustomType
CustomType(int num) : value(static_cast<double>(num)) {}

// Constructor that takes a double and converts it to CustomType


CustomType(double num) : value(num) {}

// Conversion operator to int


operator int() const {
return static_cast<int>(value);
}

// Conversion operator to double


operator double() const {
return value;
}

void display() const {


cout << "CustomType value: " << value << endl;
}
};

int main() {
int intValue = 42;
double doubleValue = 3.14;

CustomType customInt = intValue; // Convert int to CustomType


CustomType customDouble = doubleValue; // Convert double to CustomType

customInt.display();
customDouble.display();

// Convert CustomType to int and double


int backToInt = customInt;
double backToDouble = customDouble;

cout << "Converted back to int: " << backToInt << endl;
cout << "Converted back to double: " << backToDouble << endl;

return 0;
}

7(b)Write a cpp program to convert a class type data to another class type.

#include <iostream>
using namespace std;
class ClassA {
private:
int valueA;

public:
ClassA(int val) : valueA(val) {}

int getValueA() const {


return valueA;
}
};

class ClassB {
private:
int valueB;

public:
// Constructor that takes a ClassA object and converts it to ClassB
ClassB(const ClassA& objA) : valueB(objA.getValueA()) {}
void display() const {
cout << "ClassB value: " << valueB << endl;
}
};

int main() {
ClassA objectA(42);

// Convert ClassA object to ClassB object


ClassB objectB = objectA;

objectB.display();

return 0;
}

8(a) Write a program for multiple Inheritance.

#include <iostream>
#include <string>
using namespace std;
class Student {
protected:
string name;
int age;

public:
Student(const std::string& n, int a) : name(n), age(a) {}

void displayInfo() {
cout << "Name: " << name << "\nAge: " << age << endl;
}
};

class GeneralSecretary : public Student {


private:
string department;

public:
GeneralSecretary(const std::string& n, int a, const string& dept) : Student(n, a),
department(dept) {}

void displayInfo() {
Student::displayInfo();
cout << "Department: " << department << endl;
}

void manageEvents() {
cout << "Managing events as General Secretary." << endl;
}
};

int main() {
Student student("Alice", 20);
GeneralSecretary secretary("Bob", 22, "Student Affairs");

student.displayInfo();
cout << "----------" << endl;
secretary.displayInfo();
secretary.manageEvents();

return 0;
}

8(b)Write a program for Multilevel Inheritance

#include<iostream>
using namespace std;

// single base class


class A {
public:
int a;
void get_A_data()
{
cout << "Enter value of a: ";
cin >> a;
}
};

// derived class from base class


class B : public A {
public:
int b;
void get_B_data()
{
cout << "Enter value of b: ";
cin >> b;
}
};

// derived from class derive1


class C : public B {
private:
int c;

public:
void get_C_data()
{
cout << "Enter value of c: ";
cin >> c;
}

// function to print sum


void sum()
{
int ans = a + b + c;
cout << "sum: " << ans;
}
};
int main()
{
// object of sub class
C obj;

obj.get_A_data();
obj.get_B_data();
obj.get_C_data();
obj.sum();
return 0;
}

9(a)WAP to perform Virtual base class

#include<iostream>
using namespace std;

class student {
int rno;
public:

void getnumber() {
cout << "Enter Roll No:";
cin>>rno;
}

void putnumber() {
cout << "\n\n\tRoll No:" << rno << "\n";
}
};

class test : virtual public student {


public:
int part1, part2;

void getmarks() {
cout << "Enter Marks\n";
cout << "Part1:";
cin>>part1;
cout << "Part2:";
cin>>part2;
}

void putmarks() {
cout << "\tMarks Obtained\n";
cout << "\n\tPart1:" << part1;
cout << "\n\tPart2:" << part2;
}
};

class sports : public virtual student {


public:
int score;

void getscore() {
cout << "Enter Sports Score:";
cin>>score;
}

void putscore() {
cout << "\n\tSports Score is:" << score;
}
};

class result : public test, public sports {


int total;
public:

void display() {
total = part1 + part2 + score;
putnumber();
putmarks();
putscore();
cout << "\n\tTotal Score:" << total;
}
};

int main() {
result obj;

obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
}

9(b)WAP using exception handling mechanism and handle at least any 3 exceptionsin
one program.

#include <iostream>
#include <string>
using namespace std;

int main() {
try {
// Try to perform some potentially risky operations

// Division by zero exception


int num1, num2;
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;

if (num2 == 0) {
throw string("Division by zero is not allowed.");
}
int result = num1 / num2;

// Out of range exception


int myArray[3] = {1, 2, 3};
cout << "Enter an index to access the array (0-2): ";
int index;
cin >> index;

if (index < 0 || index >= 3) {


throw out_of_range("Index is out of range for the array.");
}
int value = myArray[index];

// Invalid input exception (e.g., non-integer input)


cout << "Enter a number to convert to an integer: ";
string input;
cin >> input;

int num3;
try {
num3 = stoi(input);
} catch (const invalid_argument&) {
throw string("Invalid input. Please enter a valid number.");
} catch (const out_of_range&) {
throw string("Input value is out of the range of int.");
}

cout << "Result of division: " << result <<endl;


cout << "Value at index " << index << " in the array: " << value << endl;
cout << "Converted integer: " << num3 << endl;
} catch (const std::string& error) {
cerr << "An exception occurred: " << error << endl;
} catch (const exception& error) {
cerr << "An unexpected exception occurred: " << error.what() << endl;
} catch (...) {
cerr << "An unknown error occurred." << endl;
}

cout << "Program execution completed." << endl;

return 0;
}

10(a) Write a C++ program to perform Function template

#include <iostream>
using namespace std;
template <typename T>
T add(T num1, T num2) {
return (num1 + num2);
}

int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;

// calling with double parameters


result2 = add<double>(2.2, 3.3);
cout << "2.2 + 3.3 = " << result2 << endl;

return 0;
}

10(b) Write a C++ program to perform Class template

#include <iostream>
using namespace std;

// Class template
template <class T>
class Number {
private:
// Variable of type T
T num;

public:
Number(T n) : num(n) {} // constructor

T getNum() {
return num;
}
};

int main() {

// create object with int type


Number<int> numberInt(7);
// create object with double type
Number<double> numberDouble(7.7);

cout << "int Number = " << numberInt.getNum() << endl;


cout << "double Number = " << numberDouble.getNum() << endl;

return 0;
}

11(a) Design a class in cpp to create a new file, modify the file and save the
content of the file

You might also like