Archive
C++ Program to implement Operator Overloading with binary arithmetic op (*,+,-) – Q17
Q17. Program to implement Operator Overloading -with binary arithmetic op (*,+,-):
Define a class complex with two data members real & imag.
Perform the following operations:
– Find the sum of two complex numbers
– Find the difference of two complex numbers
– Find the Product of two complex numbers
– Find the Product of a complex number with a constant
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
class Ccomplex{
private:
double real, img;
public:
Ccomplex(){}
Ccomplex(double r, double i){
real = r;
img = i;
}
friend Ccomplex operator+(Ccomplex &, Ccomplex &);
friend Ccomplex operator-(Ccomplex &, Ccomplex &);
friend Ccomplex operator*(Ccomplex &, Ccomplex &);
friend Ccomplex operator*(Ccomplex &, double);
friend ostream& operator<<(ostream &, Ccomplex &);
friend istream& operator>>(istream &, Ccomplex &);
};
Ccomplex operator+(Ccomplex &c1, Ccomplex &c2){
return Ccomplex(c1.real + c2.real, c1.img + c2.img);
}
Ccomplex operator-(Ccomplex &c1, Ccomplex &c2){
return Ccomplex(c1.real - c2.real, c1.img - c2.img);
}
Ccomplex operator*(Ccomplex &c1, Ccomplex &c2){
return Ccomplex(c1.real * c2.real, c1.img * c2.img);
}
Ccomplex operator*(Ccomplex &c1, double num){
return Ccomplex(c1.real * num, c1.img * num);
}
ostream& operator<<(ostream &Mout, Ccomplex &Oc){
return Mout<<"\n Real: "<<Oc.real<<"\n Img: "<<Oc.img;
}
void main(){
Ccomplex Oc;
Ccomplex Oc1(3.1, 4.1);
Ccomplex Oc2(4.2, 5.7);
double num= 10;
clrscr();
cout<<"\n Sum of two Complex No's:- ";
Oc = Oc1 + Oc2;
cout<<Oc;
cout<<"\n\n Difference of two Complex No's:- ";
Oc = Oc1 - Oc2;
cout<<Oc;
cout<<"\n\n Product of two Complex No's:- ";
Oc = Oc1 * Oc2;
cout<<Oc;
cout<<"\n\n Product of Complex No with Constant:-";
Oc = Oc1 * num;
cout<<Oc;
getch();
}
Output:
Sum of two Complex No’s:-
Real: 7.3
Img: 9.8
Difference of two Complex No’s:-
Real: -1.1
Img: -1.6
Product of two Complex No’s:-
Real: 13.02
Img: 23.37
Product of Complex No with Constant:-
Real: 31
Img: 41
C++ Program to implement Operator Overloading with Unary Operators (++, –) – Q16
Q16. Program to implement Operator Overloading with Unary Operators (++, –):
Define a DATE class with month, day & year as the private data member.
Implement Operator Overloading for finding the next day of given date.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
class CDATE{
private:
int dd, mm, yy;
int _days;
static int months[12];
public:
void Init(){
cout<<"\n Enter a Date (dd mm yyyy): ";
cin>>dd>>mm>>yy;
}
void operator++(){
_days = DaysThisMonth(mm, yy);
if(dd >=_days){
if(mm >= 12){
yy += 1;
mm = 1;
}
else
mm += 1;
dd = 1;
}
else
dd += 1;
cout<<"\n Next Date: "<<dd<<":"<<mm<<":"<<yy;
}
int DaysThisMonth(int, int);
};
int CDATE :: months[12] = { 31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
int CDATE :: DaysThisMonth(int mm, int yy){
if (mm != 2)
return months[mm-1];
if (yy % 4) // Not leap year
return 28;
if (yy % 100) // It is leap year
return 29;
if (yy % 400) // Not leap year
return 28;
return 29; // It is leap year
}
void main(){
CDATE dt;
clrscr();
dt.Init();
char ch;
while(ch != 'y'){
++dt;
cout<<"\t\t\t Quit (y/n) ?: ";
cin>>ch;
}
cout<<"\n\n Press Any Key to Exit ...";
getch();
}
Output:
Enter a Date (dd mm yyyy): 26 12 2003
Next Date: 27:12:2003 Quit (y/n) ?: n
Next Date: 28:12:2003 Quit (y/n) ?: n
Next Date: 29:12:2003 Quit (y/n) ?: n
Next Date: 30:12:2003 Quit (y/n) ?: n
Next Date: 31:12:2003 Quit (y/n) ?: n
Next Date: 1:1:2004 Quit (y/n) ?: n
Next Date: 2:1:2004 Quit (y/n) ?: n
Next Date: 3:1:2004 Quit (y/n) ?: n
Next Date: 4:1:2004 Quit (y/n) ?: n
Next Date: 5:1:2004 Quit (y/n) ?: y
Press Any Key to Exit …
C++ Program to implement Dynamic Class with Constructor & Destructor – Q15
Q15. Program to implement Dynamic Class with Constructor & Destructor:
Get the sum of elements of an array with user defined size, used dynamically within class using new & delete inside class constructor & destructor respectively.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
#include <string.h>
class CArray{
private:
int *arr;
int len, sum;
public:
CArray(){
sum = 0;
}
CArray(int s){
len = s;
arr = new(int[len]);
}
~CArray(){
delete(arr);
}
void getArray();
void Sum();
void Display();
};
void CArray :: getArray(){
cout<<"\n Enter "<<len<<" elements in an Array: ";
for(int i=0;i <len; i++)
cin>>arr[i];
}
void CArray :: Sum(){
for(int i=0; i<len; i++)
sum += arr[i];
}
void CArray :: Display(){
cout<<"\n Total Sum: "<<sum;
}
void main(){
int sz;
clrscr();
cout<<"\n Enter the Size of Array: ";
cin>>sz;
CArray Oarr(sz);
Oarr.getArray();
Oarr.Sum();
Oarr.Display();
getch();
}
Output:
Enter the Size of Array: 5
Enter 5 elements in an Array: 33 21 45 76 39
Total Sum: 214
C++ Program to implement Pointer to function – Q14
Q14. Program to implement Pointer to function:
Get the sum of two numbers using the concept of pointer to function.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
int sumNum(int a, int b){
return (a+b);
}
int (*ptr_f)(int, int); // pointer to function.
void main(){
int num1, num2;
clrscr();
cout<<"\n Enter 1st Number: ";
cin>>num1;
cout<<"\n Enter 2nd Number: ";
cin>>num2;
ptr_f = &sumNum; // ptr_f points to display.
cout<<"\n Sum of two Numbers: "<<ptr_f(num1, num2);
getch();
}
Output:
Enter 1st Number: 34
Enter 2nd Number: 45
Sum of two Numbers: 79
C++ Program to implement Constructor Overloading – Q13
Q13. Program to implement Constructor Overloading:
Implement the concept of Constructor Overloading with the help of not less than three different constructors within any class of your choice.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
#include <string.h>
class Cperson{
private:
char name[20];
public:
Cperson(){
cout<<"\n Constructor Called."<<endl;
strcpy(name, "Manoj");
}
Cperson(char *str){
strcpy(name, str);
}
Cperson(Cperson &per){
strcpy(name, per.name);
}
void Show_Name(){
cout<<name;
}
};
void main(){
clrscr();
Cperson Op1;
Cperson Op2("Pandey");
Cperson Op3(Op2);
cout<<"\n Op1: ";
Op1.Show_Name();
cout<<"\n Op2: ";
Op2.Show_Name();
cout<<"\n Op3: ";
Op3.Show_Name();
getch();
}
Output:
Constructor Called.
Op1: Manoj
Op2: Pandey
Op3: Pandey




