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

ASSIGNMENT-1 (1) //design The Emp Class and Display The Emp Information Get - Info and Display - Info Where Get - Info Is A Private Member Function.

This document contains C++ code for operator overloading examples. It defines three assignments with multiple programs each: Assignment 1 contains programs to overload the unary minus operator to get the negative of a number, add two times by overloading the addition operator, and concatenate two strings by overloading the addition operator. Assignment 2 contains programs to add two complex numbers using a friend function, calculate total distance by adding values from two classes, and add two matrices by defining the addition operation as a friend function. Assignment 3 contains similar examples of operator overloading for educational purposes.

Uploaded by

ashmee
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)
39 views22 pages

ASSIGNMENT-1 (1) //design The Emp Class and Display The Emp Information Get - Info and Display - Info Where Get - Info Is A Private Member Function.

This document contains C++ code for operator overloading examples. It defines three assignments with multiple programs each: Assignment 1 contains programs to overload the unary minus operator to get the negative of a number, add two times by overloading the addition operator, and concatenate two strings by overloading the addition operator. Assignment 2 contains programs to add two complex numbers using a friend function, calculate total distance by adding values from two classes, and add two matrices by defining the addition operation as a friend function. Assignment 3 contains similar examples of operator overloading for educational purposes.

Uploaded by

ashmee
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

ASMITA.B.

PRASAD
SYIT-C 6036

ASSIGNMENT-1(1)

//Design the emp class and display the emp information get_info() and display_info() where
get_info is a private member function.//

#include<iostream.h>
#include<conio.h>
class emp
{
int id,salary;
char name[10];
void get_info();
public:
void display()
{
get_info();
clrscr();
cout<<"\n\n ***** Employee Database *****\n\n" ;
cout<<"Employee ID :- "<<id<<"\n";
cout<<"Employee Name :- "<<name;
cout<<"\nEmployee Salary :- "<<salary;
}

};

void emp :: get_info()


{
cout<<"\n\n ***** Employee Database ***** \n\n" ;
cout<<"Enter Employee ID :- ";
cin>>id;
cout<<"\nEnter Employee Name :- ";
cin>>name;
cout<<"\nEnter Employee Salary :- ";
cin>>salary;
}
void main()
{
ASMITA.B.PRASAD
SYIT-C 6036

clrscr();
emp e;
e.display();
getch();
}

OUTPUT :-
ASMITA.B.PRASAD
SYIT-C 6036

ASSIGNMENT-1(2)

//Design a class student containing get_info() and display_info() function used for reading
and displaying where get_info will be a private method.//
#include<iostream.h>
#include<conio.h>
class stud
{
int roll,percentage;
char name[10];
void get_info();

public:
void display()
{
get_info();
clrscr();
cout<<"\n\n ***** Student Database *****\n\n" ;
cout<<"Student Roll No. :- "<<roll<<"\n";
cout<<"Student Name :- "<<name;
cout<<"\nStudent percentage :- "<<percentage;
}

};

void stud :: get_info()


{
cout<<"\n\n ***** Student Database ***** \n\n" ;
cout<<"Enter Student Roll No. :- ";
cin>>id;
cout<<"\nEnter Student Name :- ";
cin>>name;
cout<<"\nEnter Student Percentage :- ";
cin>>percentage;
}
void main()
{
clrscr();
stud s;
ASMITA.B.PRASAD
SYIT-C 6036

s.display();
getch();
}

OUTPUT:-
ASMITA.B.PRASAD
SYIT-C 6036

ASSIGNMENT-1(3)

//C++ program to check factorial,reverse,palindrome, Armstrong of a given number.//

#include<iostream.h>
#include<conio.h> #include<stdio.h>
class demo
{
int n,f,i,r,d,t,re;
void read();
public:
void fact();
void pal();
void arm();
void rev();

};

void demo ::read()


{
cout<<"Enter a number :- ";
cin>>n;
};

void demo :: fact()


{
f=1;
cout<<"\n\n*** To calculate Factorial of a number *** \n\n";
read();
for(i=n;i>0;i--)
{
f=f*i;
}

cout<<" Fact = "<<f;


}
ASMITA.B.PRASAD
SYIT-C 6036

void demo :: rev()


{
r=0;
d=0;
cout<<"\n\n*** To calculate reverse of a number *** \n\n";

read();

while(n!=0)
{
d=n%10;
r=(r*10)+d;
n=n/10;
}
cout<<"\nReverse of number is :- "<<r;
}
void demo :: pal()
{
flushall();
t=0;
re=0;
cout<<"\n\n*** To check for Palindrome ***\n\n ";
read();
t=n;
while(n!=0)
{
d=n%10;
re=(re*10)+d;
n=n/10;
}
cout<<"Revere of entered number is :- "<<re;

if(re==t)
cout<<"\nIt is a Palindrome";
else
cout<<"Not a Palindrome !!!\n ";

}
ASMITA.B.PRASAD
SYIT-C 6036

void demo :: arm()


{
int m,rv=0,v=0;
cout<<"\n\n *** To check for Armsrong Number *** \n\n";
cout<<"Enter a Number :- ";
cin>>m;
t = m;

while(m!=0)
{
V = m % 10;
rv = rv + (v*v*v);
m = m / 10;
}

if(rv==t)
cout<<"\nIt is an Armstrong Number\n";
else
cout<<"Not an Armstrong Number !!!\n ";

}
void main()
{

clrscr();
demo d;
d.fact();
d.rev();
d.pal();
d.arm();
getch();
}
ASMITA.B.PRASAD
SYIT-C 6036

OUTPUT:-
ASMITA.B.PRASAD
SYIT-C 6036

ASSIGNMENT-2(1)
//C++ program of friend function for adding two complex numbers using single class.//

#include<iostream.h>

#include<conio.h>

class complex
{
int r1,r2,i1,i2;
int sum_r,sum_i;

public:
void get();
friend void add(complex);

};

void complex :: get()


{
cout<<"\n\n***** Program to add 2 complex numbers *****\n\n";

cout<<"Enter Real part of 1st number :- " ;


cin>>r1;
cout<<"Enter Imaginary part of 1st number :- " ;
cin>>i1;
cout<<"Enter Real part of 2nd number :- " ;
cin>>r2;
cout<<"Enter Imaginary part of 2nd number :- " ;
cin>>i2;
}

void add(complex c)
{
c.sum_r = c.r1 + c.r2;

c.sum_i = c.i1 + c.i2;


ASMITA.B.PRASAD
SYIT-C 6036

cout<<" \n*** OutPut ***\n" ;


cout<<"Addition is = "<<c.sum_r<<" + "<<c.sum_i<<" i";

void main()
{
clrscr();
complex c;
c.get();
add(c);

getch();
}

OUTPUT:-
ASMITA.B.PRASAD
SYIT-C 6036

ASSIGNMENT-2(2)

//C++ program for adding two different distance and display its sum using two classes.//

#include<iostream.h>

#include<conio.h>

class distance2;

class distance1
{
float km;
public:

void get1()
{
cout<<"\n\nEnter Total Distance Travelled in KILOMETRES:- ";
cin>>km;
}

friend void sum(distance1,distance2);


};

class distance2
{
float m,cm;
public:

void get2()
{
cout<<"\n\nEnter Total Distance travelled in METERS:-;
cin>>m;
cout<<"\n\nEnter Total Distance travelled in CM :-;
cin>>cm;

friend void sum(distance1,distance2);


};
void sum(distance1 d1,distance2 d2)
{
ASMITA.B.PRASAD
SYIT-C 6036

float temp1 = 0,temp2 = 0;


float total1 = 0,total2 = 0;

temp1 = d1.km * 1000;


temp2 = d2.cm / 100;

total1 = temp1 + d2.m + temp2;


total2 = total1 / 1000;

cout<<"\n\nTotal distance travelled is:- "<<total2<<" kms ";


}

void main()
{
clrscr();
distance1 d1;
distance2 d2;
cout<<"\n\n ***** Program to Calculate Total Distance ***** ";
d1.get1();
d2.get2();
sum(d1,d2);
getch();
}

OUTPUT:-
ASMITA.B.PRASAD
SYIT-C 6036
ASMITA.B.PRASAD
SYIT-C 6036

ASSIGNMENT-2(3)

// C++ program for addition of two matrices from two different classes and display its sum.//

#include<iostream.h>

#include<conio.h>

class mat2;

class mat1
{
int a[5][5],i,j;

public:
void get1();
friend void add(mat1,mat2);
};

void mat1 :: get1()


{
cout<<"\n\n *** Matrix 1 *** \n";
cout<<"\nEnter elements for a 3x3 matrix 1\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}

class mat2
{
int b[5][5],i,j;

public:
void get2();
ASMITA.B.PRASAD
SYIT-C 6036

friend void add(mat1,mat2);


};

void mat2 :: get2()


{
cout<<"\n\n *** Matrix 2 *** \n";
cout<<"\nEnter elements for a 3x3 matrix 2\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
}

void add(mat1 m,mat2 n)


{
int c[5][5],i,j;
cout<<"\n\n *** OutPut *** \n\n" ;
cout<<"Addition of two matrices is :- \n";

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=m.a[i][j]+n.b[i][j];
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<c[i][j]<<" ";
}
ASMITA.B.PRASAD
SYIT-C 6036

void main()
{
clrscr();
mat1 m;
mat2 n;

m.get1();
n.get2();

add(m,n);

getch();
}

OUTPUT:-
ASMITA.B.PRASAD
SYIT-C 6036

ASSIGNMENT-3(1)
// Program for Operator Overloading using Unary minus Operator.//

#include<iostream.h>

#include<conio.h>
class minus
{
int A;
public:
void get()
{
cout <<"\nEnter a number :- ";
cin>>A;
}
void operator - ();
};

void minus :: operator - ()


{
A = -A;
cout<<"\nNegative of entered number is := "<<A;
}

void main()
{
clrscr();

cout<<"\n\n****** Program to implement";


cout<<" Operator Overloading Of Unary Minus *****\n";

minus m1,m2,m3;

m1.get();
m2.get();
m3.get();
- m1;
- m2;
ASMITA.B.PRASAD
SYIT-C 6036

- m3;

getch();
}

OUTPUT:-
ASMITA.B.PRASAD
SYIT-C 6036

ASSIGNMENT-3(2)

//C++ program to Adding the timing of Two Clocks and also pass object as argument.//

#include<iostream.h>

#include<conio.h>

class time2;

class time1
{
int hr,min;
public:
void gettime(int a,int b)
{
hr=a;
min=b;
}
friend void operator +(time1,time2);
};

class time2
{
int h,m;
public:
void getdata(int p,int q)
{
h=p;
m=q;
}
friend void operator +(time1,time2);
};

void operator +(time1 obj1,time2 obj2)


{
int minute,hour;
hour=obj1.hr+obj2.h+(obj1.min+obj2.m)/60;
minute=(obj1.min+obj2.m)%60;
cout<<"\nTotal Time from both classes:-\n";
ASMITA.B.PRASAD
SYIT-C 6036

cout<<hour<<" hrs "<<minute<<" mins";


}
void main()
{
clrscr();

time1 t1;
time2 t2;

cout<<"\n\n** Program to add timimgs of two classes **\n\n";


cout<<"Timing from clock 1 :- \n";
cout<<"hours == 100 minutes == 100 \n";
cout<<"\nTiming from clock 2 :- \n";
cout<<"hours == 50 minutes == 50 \n";

t1.gettime(100,100);
t2.getdata(50,50);

t1+t2;
getch();
}

OUTPUT:-
ASMITA.B.PRASAD
SYIT-C 6036

ASSIGNMENT-3(3)
// C++ program to Concatenate Two Strings.//

#include <iostream.h>

#include <conio.h>

#include <string.h>
class Add
{
char str[80] ;
public :
void getdata()
{
cout <<"\nEnter String1 :- " ;
cin>>str ;
}
void showdata()
{
cout<<str;
}
Add operator + (Add s)
{
Add s3 ;
strcpy ( s3.str , str) ;
strcat ( s3.str , " ") ;
strcat ( s3.str , s.str) ;
return s3 ;
}
};

void main ()
{
clrscr () ;
cout<<"\n\n ***Program to Concatenate two Strings *** \n\n";
Add a1 , a2 , a3 ;
a1.getdata () ;
a2.getdata () ;
ASMITA.B.PRASAD
SYIT-C 6036

a3 = a1 + a2 ;
cout << "\nString 1 :- " ;
a1.showdata () ;
cout << "\nString 2 :- " ;
a2.showdata () ;
cout << "\n\nConcatenation of both strings :- " ;
a3.showdata () ;
getch () ;
}

OUTPUT:-

You might also like