Thursday, 28 December 2017

CPP-Slip2


Q.1)Write a C++ program using class which contains two data members of type integer. Create and initialize the object using default constructor, parameterized constructor and parameterized constructor with default value. Write a member function to display maximum from given two numbers for all objects.       

#include<stdio.h>
#include<conio.h>
#include<iostream.h>

class max
{
 int a,b;

 public:

 max() //default constructor
{  
}

max(int a1,int b1) //parameterized constructor
{
 a=a1;
 b=b1;
 }
void maximum()
 {
  if(a>b)
 cout<<endl<<a<<"is greater";
 else
  cout<<endl<<b<<"is greater";
 }
};

void main()
{
 int c,d;
 clrscr();
cout<<endl<<"enter number";
cin>>c;
cin>>d;
max b(c,d);
 b.maximum();
 getch();
}

or

void main()
{
 clrscr();
max b(3,4);
 b.maximum();
 getch();
}


Q.2) Create a class ComplexNumber containing members as:
- real
- imaginary

Write a C++ program to calculate and display the sum of two complex numbers. (Use friend function and return by object)   

#include<iostream.h>
#include<conio.h>
class complex
{
 float real;
 float imaginary;
 public:
complex(float a=0.0,float b=0.0)
{
  real=a;
 imaginary=b;
}
 void display()
{
 cout<<"\n real number :"<<real;
 cout<<"\n imaginary number:"<<imaginary<<"i";
 }
friend complex operator +(complex,complex);
};

complex operator +(complex c1,complex c2)
{
complex temp;
temp.real=c1.real+c2.real;
temp.imaginary=c1.imaginary+c2.imaginary;
return temp;
}

void main()
{
float n1,n2,n3,n4;
 clrscr();

cout<<"\n Enter 1st real and imaginary no :- ";
 cin>>n1;
 cin>>n2;
 complex c1(n1,n2);

 cout<<"\n Enter 2nd real and imaginary no :- ";
 cin>>n3;
 cin>>n4;
 complex c2(n3,n4);

cout<<"\n 1st complex no is :  ";
c1.display();
cout<<"\n 2nd complex no is :  ";
 c2.display();

 complex c3;
 c3=c1+c2;
 cout<<"\n After adding of float and imaginary no are :";
 c3.display();
 getch();
}

or

void main()
{
clrscr();
complex c1(3,4);
complex c2(2,3);
complex c3;
c3=c1+c2;
c1.display();
c2.display();
c3.display();
getch();

}