Oops Lab Manual-1
Oops Lab Manual-1
Oops Lab Manual-1
#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
return 0;
}
1(c) Write a program in C++ for call by reference and return by reference.
#include <iostream>
using namespace std;
int main() {
int num1 = 5;
int num2 = 10;
cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
int num3 = 7;
cout << "Before increment: num3 = " << num3 << endl;
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;
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;
}
int main() {
Time time1, time2, sum;
// Set time for time1 and display it
time1.setTime(2, 45);
cout << "Time 1:" << endl;
time1.displayTime();
return 0;
}
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;
}
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
void display();
};
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{
student s(1001,"Ram",10000);
s.display();
return 0;
}
#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();
};
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();
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;
};
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);
};
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;
}
}
};
int main ()
{
// create object for the class Car
Car c1;
// call inp() function to insert values
c1. inp ();
c1. 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;
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)) {}
int main() {
int intValue = 42;
double doubleValue = 3.14;
customInt.display();
customDouble.display();
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) {}
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);
objectB.display();
return 0;
}
#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;
}
};
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;
}
#include<iostream>
using namespace std;
public:
void get_C_data()
{
cout << "Enter value of c: ";
cin >> c;
}
obj.get_A_data();
obj.get_B_data();
obj.get_C_data();
obj.sum();
return 0;
}
#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";
}
};
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;
}
};
void getscore() {
cout << "Enter Sports Score:";
cin>>score;
}
void putscore() {
cout << "\n\tSports Score is:" << score;
}
};
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
if (num2 == 0) {
throw string("Division by zero is not allowed.");
}
int result = num1 / num2;
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.");
}
return 0;
}
#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;
return 0;
}
#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() {
return 0;
}
11(a) Design a class in cpp to create a new file, modify the file and save the
content of the file