Program to Print Sum of two numbers using Class in C++.

 

 

Class:  “Format allows you to combine data and the funtion in a single unit is known as class.”

“In this program we find sum of two numbers by using Class.

 

 

Let’s write a program to print Sum of two numbers in c++ by using Class.

 

Step 1: Let’s open turboC++ and write the following Code :

 

// Code:

//Add Preprocessor

 

#include<iostream.h>

#include<conio.h>

class sum

{

private:

long int a,b,c;

public:

void input();

void display();

};

void sum :: input()

{

cout<<”enter first number:”;

cin>>a;

cout<<”enter second number :”;

cin>>b;

c=a+b;

}

void sum :: display()

{

cout<<”sum :”<<c;

}

void main()

{

sum s;

s.input();

s.display();

getch();

}

 

 

Step 2: Run the program and see output.

 

Output:

 

sum-output

 

Happy coding.

 

 

 

 

 

Tags: ,