Classes & Objects : Programs

******************************PROGRAM 1******************************

//Program to add two numbers using function

#include<iostream>
 using namespace std;
 int add(int n1,int n2)
 {

int res=n1+n2;
 return res;

}
 int main()
 {

int num1,num2;
 cout<<"Enter two numbers\n";
 cin>>num1>>num2;
 add(num1,num2);
 cout<<"Addition of "<<num1<<" and "<<num2 >>" is "<<res;
 return 0;

}

***************************PROGRAM 2***********************************
//Program to demonstrate Complex class using 
//default and parameterized constructor

#include<iostream>
 using namespace std;

class Complex
 {
 private:
 float real,img;

public:
 //Declaration of Member functions in class

Complex(); //Default Constructor
 Complex(float r,float i);    //Parameterized constructor
 void show(); //Method to display member variables
 void getData(); //Method to accept values for member variables

};
 //Definition of constructor
 Complex::Complex()
 {
 real=0.0;
 img=0.0;
 }
 Complex::Complex(float r,float i)
 {
 real=r;
 img=i;
 }
 void Complex::show()
 {
 cout<<"Complex number is "<<real<<"+"<<img<<"i";
 }
 void Complex::getData()
 {
 cout<<"Enter real and img part of complex number\n";
 cin>>real>>img;
 }
 int main()
 {

Complex c1;
 c1.getData();
 c1.show();
 Complex c2(10.23,34.4);
 //c2.getData();
 c2.show();
 return 0;

}

*********************************PROGRAM 3*****************************

//Program to demonstrate overloading of functions

#include<iostream>
 using namespace std;

//Prototype declaration
 int add(int,int);
 void add(float,int);
 int add(int);
 float add(int,float,int);

int main()
 {

//function 1 called
 int res=add(10,20);
 cout<<"Addition is "<<res;
 //function 2 called
 add(10.5,33);

//function 3 called
 res=add(10);
 cout<<"Addition is "<<res;
 //function 4
 float fres=add(10,3.4,45);
 cout<<"Addition is "<<fres;
 return 0;

}
 int add(int a,int b)
 {
 return a+b;
 }
 void add(float f,int i)
 {
 cout<< f+i;
 }
 int add(int a)
 {
 return a+a;
 }
 float add(int n1,float n2,int n3)
 {
 return n1+n2+n3;
 }

******************************PROGRAM 4********************************

//Demonstration of static variables and functions

#include<iostream>
 using namespace std;
 class stat
 {

static int count;

void display()
 {

cout<<"\nCounter = "<<count;

}
 static void inc();
 };
 int stat::count=1;
 void stat::inc()
 {
 count++;
 }
 int main()
 {

stat s1,s2,s3;
 stat::inc();
 s1.display();
 stat::inc();
 s2.display();
 stat::inc();
 s3.display();

return 0;

}

********************************PROGRAM 5******************************

//Program to demonstrate new and delete keywords in user defined 
//String class with default, parameterized and copy constructor and destructor.
// It also demonstrates how to pass object as parameter to function

#include<iostream>
 #include<string.h>
 using namespace std;
 class String
 {
 private:

static int count;
 int length;
 char *name;

public:

String(); //Default constructor declaration
 String(char *n); //Parameterized constructor declaration
 String(String &s); //Copy constructor declaration
 ~String(); //Destructor declaration
 void displayString(); //Method to display string output
 void join(String &,String &); //Method to pass object as parameter

};
 int String::count;

//Default constructor
 String::String()
 {

length=0;
 name=new char[length+1];

}

//Parameterized constructor with n as value of newly created object
 String::String(char *n)
 {

length=strlen(n); //find length of string
 name=new char[length+1]; //allocate memory to name
 strcpy(name,n); //copy contents from n to name

}

//Copy Constructor to copy contents of one object to other object
 String::String(String &s)
 {

length=s.length;
 name=new char[length+1];
 strcpy(name,s.name);

}

//Destructor to delete memory assigned using new keywork
 String::~String()
 {

cout<<"\n In destructor"<<count++;
 length=0;
 delete name;   //Deallocate memory

}
 void String::displayString()
 {
 cout<<"\nString is "<<name<<endl;
 }

//Concat values of two string objects
 void String:: join(String &s1,String &s2)
 {

length=s1.length+s2.length;   //find length of merged string
 delete name; //delete memory allocated to name
 name=new char[length+1];  //assign memory using new keyword
 strcpy(name,s1.name);  // copy first string in to output string
 strcat(name,s2.name);  // merge second string in to output string

}
 int main()
 {

//Declare various objects of String class using different variables
 char *msg="How are you?";
 String s1("Hello!");
 String s2("Welcome All!");
 String s3(msg);
 String s4;
 s4.join(s1,s2);
 String s5;
 s5.join(s4,s3);
 s1.displayString();
 s2.displayString();
 s3.displayString();
 s4.displayString();
 s5.displayString();
 //Demonstration of block level access of variable

{

String s(s5);
 s.displayString();

}
 return 0;

}

*****************************PROGRAM 6*********************************

//Find roots of Quadratic Equation : Simple Program

#include<iostream>
 #include<cmath>
 #include<cstdlib>
 #include<cstring>
 using namespace std;
 int main()
 {

double a,b,c,x1,x2;
 char x;
 cout<<"enter the value of a=";
 cin>>a;
 cout<<"enter the value of b=";
 cin>>b;
 cout<<"enter the value of c=";
 cin>>c;
 cout<<"the quadratic equation is"<<a;
 cout<<"*x*x+"<<b;
 cout<<"*x+"<<c<<endl;
 if (a==0 && b==0)

cout <<"not a valid equation";

if  (a==0 && b!=0)
 {

x1=-(c/b);
 cout<<endl;
 cout<<"root="<<x1;
 cout<<endl;

}
 if ((b*b-4*a*c)>0)
 {

x2=(b*b)-(4*a*c);
 x1=-b+sqrt(x2);
 cout<<"root="<<x1<<endl;

}
 if ((b*b-4*a*c)<0)
 {

cout<<"not a real root"<<endl;

}
 system("pause");
 return 0;

}

Note: Detailed program of Quadratic Equation is given in LAB ASSIGNMENT 3.