How to write a C++ program with class?
It’s very simple to write a C++ program with a class. We need to follow the following steps;
It’s very easy to include a class with a C++ main program. Just follow the following steps;
Step 1: Begin with header files.
Step 2: adding the namespace.
Step 3: Declaration and definition of the class.
Step 4: Starting the main function.
Step 5: In the main function, creates the one or many objects of the class as you required.
Step 6: Call the functions of the class in the main function with the help of class objects.
Let’s see it with an example.
Example to write a C++ program with class
Write a program in C++ to find the sum of the series 1 +11 + 111 + 1111 + .. n terms with class.

#include <iostream>
using namespace std;
class abc
{
public:
int n;
int a()
{
cout<<"Input the number of terms : ";
cin>>n;
int s=0;
int t=1,i=1;
for(i=1;i<=n;i++)
{
cout<<" "<<t<<" ";
if (i<n)
{
cout<<"+";
}
s=s+t;
t=(t*10)+1;
}
cout<<endl<<"The Sum is :"<<s;
}
};
int main()
{
abc e;
e.a();
}
Write a program in C to convert an Octal number to a Decimal number without using an array, function, and while loop with class.
#include<iostream>
#include<math.h>
using namespace std;
class a
{
private:
int decimal_number=0 , remainder, octal_number;
int count;
public:
int input()
{
cout<<"Enter an octal number:";
cin>> octal_number;
}
int print()
{
decimal_number=0;
for(count=0;octal_number>0;count++)
{
remainder = octal_number%10;
decimal_number = decimal_number+ remainder*pow(8,count);
octal_number=octal_number/10;
}
cout<<"decimal equivalent of"<<decimal_number;
}
};
int main()
{
a obj;
obj.input();
obj.print();
}
Write a program in C to find the number and sum of all integer between 100 and 200 which are divisible by 9 with class.
//Write a program to find the number and sum of all integer between 100 and 200 which are divisible by 9.
//simple program
#include<iostream>
using namespace std;
class number
{
private:
int i,sum=0;
public:
int show_num()
{
for(i=101; i<200; i++)
if(i%9==0)
{
cout<<"numbers divisble by 9 is= "<<i<<endl;
sum=sum+i;
}
cout<<"sum of all numbers divisible by 9 is= "<<sum<<endl;
}
};
int main()
{
number f;
f.show_num();
}
