C++ | Introduction to class

Introduction

This is tutorial for introduction to classes in c++. The program is given below that uses class to add two numbers. The program is extendable. Go enjoy the program. Lets begin……

Program to add two numbers using class in c++

#include<iostream.h>
#include<conio.h>
//create a class
class add
{
//declare int as private
private:
int a,b,c;
//declare all functions as public
public:
void getdata(void)
{
cout<<“Enter the two numbers”<<endl;
cin>>a>>b;
}
void sum(void)
{
c=a+b;
}
void print(void)
{
cout<<“Addition = “<<c;
}
};
void main()
{
//clear the screen.
clrscr();
//declare class add x
add x;
//call all functions
x.getdata();
x.sum();
x.print();
//get character
getch();
}

Output

Enter the two numbers
12
16
Addition = 28

Extending it

The program can be extended by using the concept in any program. Like for ex. you can create a same program for subtraction, multiplication,division,etc. Go ahead extend it……

  • Remember this is a basic part of c++ which can be used in any program….

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Declare class.
  3. Add void main.
  4. Start program by first clearing the screen.
  5. Declare the x as your class name.
  6. Calculate the sum.
  7. Print the sum.

At the end

You learnt creating the C++ program for Introduction to classes in c++. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s