Object Oriented Programming

Program-1:
Calculate CGPA of a Student by using a Class:
Objective: To find out Total CGPA of a Student.
Source Code:
#include <iostream.h>
class student
{
 char name[30];
 int roll;
 float gpa1,gpa2,gpa3,cgpa;
public:
 void getdata(void);
 void display(void);
};
void student::getdata(void)
{

cin>>name;

cin>>roll;

cin>>gpa1;

cin>>gpa2;

cin>>gpa3;
}
void student::display(void)
{
 cgpa=(gpa1+gpa2+gpa3)/3.0;
  cout<<name<<roll<<cgpa<<”\n”;
}
void main()
{
 student s;
 s.getdata();
 s.display();
 }
Input:
Zakir
100
10
20
30
Output:
Zakir10020
Press any key to continue

Conclusion: I had successfully solved the problem and got the right output.

Program-2:
Write a Program Using a Class.
Objective: How to store one person information.
Source Code:
#include “iostream.h”
class person
{
 char name[30];
 int age;
public:
 void getdata(void);
 void display(void);
};
void person ::getdata(void)
{
 cout<<”Enter Name: “;
 cin>>name;
 cout<<”Enter Age: “;
 cin>>age;
}
void person ::display(void)
{
 cout<<”\nName: “<<name;
 cout<<”\nAge: “<<age;
}

int main()
{
 int tt;
 person p;
 p.getdata();
 p.display();
 cin>>tt;
 return 0;
}
Input:
Enter Name: Zakir
Enter Age: 22
Output:
Name: Zakir
Age: 22

Conclusion: I had successfully solved the problem and got the right output.

 

 

 

Program-3:
Write a program to using a Constructor.
Objective: How to add two complex number.
Source Code:
# include <iostream.h>
class complex {
 float x,y;
public:
 complex () {}
 complex(float a) { x=y=a;}
 complex(float real, float imag)
 {
  x=real;
  y=imag;
 }
 friend complex sum(complex c1, complex c2);
 friend void show(complex c);
};
complex sum(complex c1, complex c2)
{
 complex c3;
 c3.x=c1.x+c2.x;
 c3.y=c1.y+c2.y;
 return(c3);
}
void show (complex c)
{
 cout<<c.x<<”+j”<<c.y<<endl;
}
void main()
{
 complex c1(2.7,3.5);
 complex c2(1.6);
 complex c3;
 c3= sum(c1,c2);
 show(c1);
 show(c2);
 show(c3);
}
Output:
2.7+j3.5
1.6+j1.6
4.3+j5.1
Press any key to continue

Conclusion: I had successfully solved the problem and got the right output.

 
Prrogram-4:
Write a program using Destructor.
Objective: How to works a destructor?
Code:
#include<iostream.h>
int count =0;
class alpha
{
public:
alpha()
{
count++;
cout<<”\nNo. of object created “<<count;
}
~alpha()
{
 cout<<”\nNo. of object destroyed “<<count;
 count–;
}
};
void main()
{
cout<<”\n\nEnter Main\n”;
alpha a1,a2,a3,a4;
{
cout<<”\n\nEnter Block1\n”;
alpha a5;
}
{
cout<<”\n\nEnter Block2\n”;
alpha a6;
}
cout<<”\n\nEnter Main\n”;
}
Output:
Enter Main

No. of object created 1
No. of object created 2
No. of object created 3
No. of object created 4

Enter Block1

No. of object created 5
No. of object destroyed 5

Enter Block2

No. of object created 5
No. of object destroyed 5
Enter Main

No. of object destroyed 4
No. of object destroyed 3
No. of object destroyed 2
No. of object destroyed 1Press any key to continue

Conclusion: I had successfully solved the problem and got the right output.

Program-5:
Write a program which find out the Factorial value.
Objective: To find out factorial value of any number.
Source Code:
# include “iostream.h”
void main()
{
 cout<<”Factorial!\n”;
 int f=1, n, i;
 cin>>n;
 for(i=1;i<=n;i++)
  f=f*i;
 cout<<f;
 cout<<endl;
}
Input:
Factorial!
10
Output:
3628800
Press any key to continue

Conclusion: I had successfully solved the problem and got the right output.

Program-6:
Write a program using friend function.
Objective: How to works a friend function.
Source Code:
#include<iostream.h>
class sample
{
int u;
int v;
public:
void setvalue()
{u=47;v=40;}
friend float mean(sample s);
};
float mean(sample s)
{
return float(s.u+s.v)/2.0;
}
main()
{
sample X;
X.setvalue();
cout<<”\n    Mean value= “<<mean(X)<<”\n”;
return 0;
}
Output:
    Mean value= 43.5
Press any key to continue

Conclusion: I had successfully solved the problem and got the right output.

Program-7:
Write a program using Function Overloading.
Objective: How to works a Overloading function.
Source Code:
#include “iostream.h”
int volume (int s)
{
 return (s*s*s);
}
double volume (double r, int h)
{
 return (3.14*r*r*h);
}
long volume (long l, int b, int h)
{
 return (l*b*h);
}
main()
{
 cout<<volume(5);
 cout<<volume(5.0, 2);
 cout<<volume(2,1,10);
}

Output:
12515720Press any key to continue

Conclusion: I had successfully solved the problem and got the right output.

 

 

 

Program-8:
Write a program using In Line Function.
Objective: How to multiple two number by using in line function.
Source Code:
# include “iostream.h”
inline float mul(float x, float y)
{
 return(x*y);
}
void main()
{
 float a= 5.0, b=5.0;
 cout<<mul(a,b);
}

Output:
25Press any key to continue

Conclusion: I had successfully solved the problem and got the right output.

Program-9:
Write a program using Multiple Class.
Objective: How to works a multiple class.
Source Code:
# include “iostream.h”
class M
{
protected :
 int m;
public:
 void get_m(int x)
 {
  m=x;
 }
};
class N
{
protected:
 int n;
public:
 void get_n(int y)
 {
  n=y;
 }
};
class P:public M, public N
{
public:
 void display (void)
 {
  cout<<m<<”\n”;
  cout<<n<<”\n”;
  cout<<”—-”<<”\n”;
  cout<<m*n<<endl;
 }
};
void main()
{
 P x;
 x.get_m(10);
 x.get_n(20);
 x.display();
}

Output:
10
20
—-
200
Press any key to continue

Conclusion: I had successfully solved the problem and got the right output.

Program-10:
Write a program using Operator Overloading.
Objective: How to add two complex number by using Operator Overloading.
Source Code:
# include <iostream.h>
class complex
{
 float x;
 float y;
public:
 complex() {}
 complex(float a) { x=y=a;}
 complex (float real, float imag)
 {
 x=real; y=imag;
 }
 friend complex sum(complex c1, complex c2);
 friend void show(complex c);
};
complex sum(complex c1, complex c2)
{
 complex c3;
 c3.x=c1.x+c2.x;
 c3.y=c2.y+c2.y;
 return(c3);
}
void show(complex c)
{
 cout<<c.x<<”+j”<<c.y<<endl;
}
void main()
{
 complex c1(2.7, 3.5);
 complex c2(1.6);
 complex c3;
 c3= sum(c1, c2);
 show(c1);
 show(c2);
 show(c3);
}

Output:
2.7+j3.5
1.6+j1.6
4.3+j3.2
Press any key to continue

Conclusion: I had successfully solved the problem and got the right output.

One Response

  1. Sir i want to learn about c++ with question solution by balagurushamy.Sir please email me with all status of c++.
    Sir i will waiting your massage.

    your sencearly
    Himanshoo

Leave a Reply