Oops Lab File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

OBJECT ORIENTED PROGRAMMING LAB

ETCS-258

Faculty Name: Ms. Karuna Middha Student Name: Chirag Goyal

Roll No. : 02096402719

Group : 3C12

Maharaja Agrasen Institute of Technology, PSP Area,

Sector – 22, Rohini, New Delhi – 110086


Experiment 1
 Write a program to find whether a number is prime or not.

Source Code:
#include<iostream>
using namespace std;
int main()
{
int n,count=0;;

cout<<"\n\tENTER A NUMBER=";
cin>>n;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
count++;
break;
}
}

if(count==0)
cout<<n<<" is a prime number\n";
else
cout<<n<<" is not a prime number\n";
return 0;
}
Output:

Experiment 2
 Write a program to take name, address as character string, age as int,
salary as float and contains inline function to set the values and display it.

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

class details {
public:
int age;
char name[20];
char address[40];
float salary;

inline void getData();


inline void showData();
};

void details::getData(){

cout<<"\nName : "; cin>>name;


cout<<"Age : "; cin>>age;
cout<<"Salary : "; cin>>salary;
cout<<"Address : "; cin>>address;
}

void details::showData(){

cout<<"\nName : "<<name;
cout<<"\nAge : "<<age;
cout<<"\nSalary : "<<salary;
cout<<"\nAddress : "<<address;
}

int main()
{
details s1,s2,s3;
cout<<"\nEnter the details of s1 as mentioned::";
s1.getData();
cout<<"\nEnter the details of s2 as mentioned::";
s2.getData();
cout<<"\nEnter the details of s3 as mentioned::";
s3.getData();
cout<<"\n\nDetails of s1 are as follows :";
s1.showData();
cout<<"\n\nDetails of s2 are as follows :";
s2.showData();
cout<<"\n\nDetails of s3 are as follows :";
s3.showData();

return 0;
}

Output:

Experiment 3
 Using the concept of function overloading, write function for calculating
area of triangle, circle and rectangle.

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

int area(int l,int b){


return (l*b);
}

float area(float r){


return (3.14*r*r);
}

float area(float b, float h){


return(0.5*h*b);
}

int main()
{
int length,breadth;
float radius,base,height;

cout<<"\n\tEnter the length and breadth of a rectangle : ";


cin>>length>>breadth;
cout<<"\tArea of rectangle : "<<area(length,breadth)<<endl;

cout<<"\n\tEnter the radius of circle : ";


cin>>radius;
cout<<"\tArea of circle : "<<area(radius)<<endl;

cout<<"\n\tEnter the base and height of triangle : ";


cin>>base>>height;
cout<<"\tArea of triangle : "<<area(base,height)<<endl;

return 0;
}

Output:

Experiment 4
 Create a class Student, which have data members as name, branch, roll
no., age, sex, five subjects. Display the name of student & his percentage
who has more than 70%.
Source Code:
#include<iostream>
using namespace std;

class student {

int marks[5];
char name[20];
char branch[10];
float rollno;
int age;
char sex;
public:
inline void getData();
void check();
inline void showData(int);
};

void student::getData(){

cout<<"\nName : "; cin>>name;


cout<<"Rollno : "; cin>>rollno;
cout<<"Branch : "; cin>>branch;
cout<<"Marks in 5 subjects :";
for(int i=0;i<5;i++)
cin>>marks[i];
}

void student::check(){
int tm=0;
for(int i=0;i<5;i++)
{
tm+=marks[i];
}
if(tm/5 >= 70)
showData(tm);

void student::showData(int tm){

cout<<"\n\nName : "<<name;
cout<<"\nRollno : "<<rollno;
cout<<"\nBranch : "<<branch;
cout<<"\nPercentage Marks : "<<tm/5;

int main()
{
student s1,s2,s3,s4;
cout<<"\nEnter the details of Student1 as mentioned::";
s1.getData();
cout<<"\nEnter the details of Student2 as mentioned::";
s2.getData();
cout<<"\nEnter the details of Student3 as mentioned::";
s3.getData();
cout<<"\nEnter the details of Student4 as mentioned::";
s4.getData();

cout<<"\n\nDetails of the Students getting aggregate 70 or more are :";


s1.check();
s2.check();
s3.check();
s4.check();

return 0;
}

Output:

Experiment 5
 Write a program for multiplication of two matrices using OOP.

Source Code:
Experiment 6
 To create a class TIME with members hours, minutes and seconds. Take
input, add two???time objects and passing objects to function and display
the result
Source Code:
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
int seconds;

public:

void getTime()
{
cout << "Enter time\n:" << endl;
cout << "Hours ";
cin>>hours;
cout << "Minutes ";
cin>>minutes;
cout << "Seconds ";
cin>>seconds;
}

void putTime()
{
cout << endl;
cout << "Time after add: ";
cout << hours << ":" << minutes << ":" << seconds << endl;
}

void addTime(Time T1,Time T2)


{
this->seconds=T1.seconds+T2.seconds;
this->minutes=T1.minutes+T2.minutes + this->seconds/60;;
this->hours= T1.hours+T2.hours + (this->minutes/60);
this->minutes %=60;
this->seconds %=60;
}
};
int main()
{
Time T1,T2,T3;
T1.getTime();
T2.getTime();
T3.addTime(T1,T2);
T3.putTime();
return 0;
}

Output:
Experiment 7
 To create a function power to raise a number m to a power n. The
function takes double value for m and integer value for n. Use default
value for n to make the function. Calculate the squares when this
argument is omitted.

Source Code:

Experiment 8
 Write a program to find the biggest of three number using Friend
Function.

Source Code:
#include<iostream>
using namespace std;
class Test {
private:
int x, y, z;
public:

void input() {
cout << "Enter three numbers:";
cin >> x >> y>>z;
}
friend void find(Test t);
};

void find(Test t)
{
if (t.x > t.y && t.x > t.z) {
cout << "Largest is:" << t.x;
}
else if (t.y > t.z) {
cout << "Largest is:" << t.y;
}
else {
cout << "Largest is:" << t.z;
}
}

int main()
{
Test t;
t.input();
find(t);
return 0;
}

Output:
Experiment 9
 Write a program to demonstrate the use of friend function with Inline
assignment.

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

class base {
int val1, val2;
public:

void get() {
cout << "Enter two values:";
cin >> val1>>val2;
}
inline friend float mean(base ob);
};
float mean(base ob) {
return float(ob.val1 + ob.val2) / 2;
}

int main()
{

base obj;
obj.get();
cout << "\n Mean value is : " << mean(obj);

Output:
Experiment 10
 Write a program to find the sum of two numbers declared in a class and
display the numbers and sum using friend class.

Source Code:
#include <iostream>
using namespace std;
class B;
class A
{
int value;
public:
void getA()
{
cout<<"Enter 1st number : ";
cin>>value;
}
friend int sum(A, B);
};
class B
{
int value;
public:
void getB()
{
cout<<"Enter 2nd number : ";
cin>>value;
}
friend int sum(A, B);
};

int sum( A v1, B v2 ){


return (v1.value + v2.value);
}

int main()
{
A a;
B b;
a.getA();
b.getB();
cout << "Sum : " << sum( a, b ) << endl;
return 0;
}
Output:
Experiment 11
 Write a program to find the greatest of two given numbers in two
different classes using friend function.

Source Code:
#include<iostream>
using namespace std;
class A;
class B;
class A
{
int a = 10;
public:
friend int great_num(A a,B b);
void getA() {
cout<<"Enter the Ist number ";
cin>>a;
}
};
class B
{
int b = 20;
public:
friend int great_num(A a,B b);
void getB() {
cout<<"Enter the IIst number ";
cin>>b;
}
};

int great_num(A x,B y) {


return (x.a>y.b)?x.a:y.b;
}

int main() {
A num1;
B num2;
num1.getA();
num2.getB();
cout << "\nGreatest of the two numbers is : <great_num(num1,num2)<<endl;
}

Output:
Experiment 12
 Write a program to enter any number and find its factorial using
constructor.

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

class Factorial {
int num;
int fact,i;
public:

Factorial (int n) {
num = n;
fact = 1;
for (i = 1; i <= num; i++) {
fact = fact*i;
}
}

void display() {
cout << "Factorial is : " << fact;
}
};
int main() {

int n;
cout<<"Enter a number : ";
cin>>n;
Factorial obj1(n);
obj1.display();

return 0;
}

Output:
Experiment 13
 Write a program to generate a Fibonacci series using Copy Constructor.

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

class Fibonacci
{
int f,f1,f2,m;
public:

Fibonacci(int n)
{
m=n;
}
Fibonacci( Fibonacci &x)
{
x.f1=0;
x.f2=1;
cout<<"\n the required Fibonacci series is as follows: \n\n ";
cout<<" "<<x.f1<<" "<<x.f2;

for(int i=2;i<=x.m;i++)
{
x.f=x.f1+x.f2;
cout<<" "<<x.f;
x.f1=x.f2;
x.f2=x.f;
}
}
};

int main()
{
int n;
cout<<"\n Enter the length of series:";
cin>>n;

Fibonacci A(n);
Fibonacci B(A);
return 0;

Output:
Experiment 14
 Write a program to perform addition of two complex numbers using
constructor overloading. The first constructor which takes no argument is
used to create objects which are not initialized, second which takes one
argument is used to initialize real and imag parts to equal values and third
which takes two argument is used to initialized real and imag to two
different values.

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

class Complex {

int real, img;


public:

Complex()
{
real=0;
img=0;
}

Complex(int temp)
{
real=temp;
img=temp;
}
Complex(int tempReal, int tempImg)
{
real=tempReal;
img=tempImg;
}

void addComp(Complex C1, Complex C2)


{
real=C1.real+C2.real;
img=C1.img+C2.img;

void display()
{
cout<<real<<" +i"<<img<<endl;
}
};

int main()
{

Complex C1(3, 2);


Complex C2(9);
cout<<"\n\tComplex number 1 : ";
C1.display();
cout<<"\n\tComplex number 2 : ";
C2.display();

Complex C3;
C3.addComp(C1, C2);
cout<<"Sum of complex number : ";
C3.display();

return 0;
}

Output:

You might also like