C++ programming language is a powerful and widely used object-oriented programming language. Given below are some C++ programs.
C++ language programs
C++ hello world program
Hello world C++ program
#include <iostream> // Declaration of header file
#include<conio.h> // Declaration of header file
using namespace std; // Using std namespace
int main() // Function main through which program execution begins
{
cout << "Hello World"; // Displaying "hello world"
{
cout << "Hello World"; // Displaying "hello world"
return 0; // Returning 0 indicates success to the operating system
}
}
C++ hello world program using a class
#include<iostream>
#include<conio.h>
using namespace std;
// Creating class
class Message
{
public:
{
public:
void display() {
cout << "Hello World";
}
};
cout << "Hello World";
}
};
int main()
{
Message t; // Creating an object
t.display(); // Calling function
{
Message t; // Creating an object
t.display(); // Calling function
return 0;
}
}
C++ programming code
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int a, b, c;
cout << "Enter two integers to add\n";
cin >> a >> b;
{
int a, b, c;
cout << "Enter two integers to add\n";
cin >> a >> b;
c = a + b;
cout <<"Sum of the numbers: " << c << endl;
return 0;
}
cout <<"Sum of the numbers: " << c << endl;
return 0;
}
C++ addition program using class
#include <iostream>
#include<conio.h>
using namespace std;
class Mathematics {
int x, y;
int x, y;
public:
void input() {
cout << "Input two integers\n";
cin >> x >> y;
}
void input() {
cout << "Input two integers\n";
cin >> x >> y;
}
void add() {
cout << "Result: " << x + y;
}
cout << "Result: " << x + y;
}
};
int main()
{
Mathematics m; // Creating an object of the class
{
Mathematics m; // Creating an object of the class
m.input();
m.add();
m.add();
return 0;
}
}
We create Mathematics class with two functions input and add. Function input is used to get two integers from a user, and function add performs the addition and displays the result. Similarly, you can create more functions to subtract, multiply, divide.
C++ program to reverse a number
C++ program to reverse a number using a class. It can be used to check if an integer is a palindrome or not.
C++ program to reverse a number
#include <iostream>
#include<conio.h>
using namespace std;
class Operations
{
long c;
{
long c;
public:
void inputNumber()
{
cout << "Input a number\n";
cin >> c;
}
void inputNumber()
{
cout << "Input a number\n";
cin >> c;
}
long reverseNumber()
{
long invert = 0;
{
long invert = 0;
while (c != 0)
{
invert = invert * 10;
invert = invert + c%10;
c = c/10;
}
{
invert = invert * 10;
invert = invert + c%10;
c = c/10;
}
return invert;
}
}
};
int main()
{
long result;
{
long result;
Operations t;
t.inputNumber();
result = t.reverseNumber();
t.inputNumber();
result = t.reverseNumber();
cout << "Number obtained on reversal = " << result;
return 0;
}
}
C++ class program example: In our program, we create a class named programming with one variable and two functions. In the main function, we create an object of this class and call these functions.
C++ programming code
#include<iostream>
#include<conio.h>
using namespace std;
class programming
{
private:
int variable;
public:
void input_value()
{
cout << "In function input_value, Enter an integer\n";
cin >> variable;
}
void output_value()
{
cout << "Variable entered is ";
cout << variable << "\n";
}
};
{
private:
int variable;
public:
void input_value()
{
cout << "In function input_value, Enter an integer\n";
cin >> variable;
}
void output_value()
{
cout << "Variable entered is ";
cout << variable << "\n";
}
};
main()
{
programming object;
object.input_value();
object.output_value();
//object.variable; Will produce an error because variable is private
return 0;
}
{
programming object;
object.input_value();
object.output_value();
//object.variable; Will produce an error because variable is private
return 0;
}
C++ constructor example
C++ constructor example program: Constructor are functions having name as that of the class. They do not have return type and are used to initialize objects.
C++ constructor program example
#include <iostream>
#include<conio.h>
using namespace std;
class Game {
private:
int goals;
public:
// constructor used to initialize
Game() {
goals = 0;
}
// constructor used to initialize
Game() {
goals = 0;
}
// return score
int getGoals() {
return goals;
}
return goals;
}
// increment goal by one
void incrementGoal() {
goals++;
}
};
goals++;
}
};
int main() {
Game football;
Game football;
cout << "Number of goals when game is started = " << football.getGoals() << endl;
football.incrementGoal();
football.incrementGoal();
football.incrementGoal();
cout << "Number of goals a little later = " << football.getGoals() << endl;
return 0;
}
}
The Game class contains a member goals which stores the number of goals. Game() constructor is used to initialize the number of goals which are zero initially. Game class object football is created and number of goals are printed just after the object is created and goals are incremented using incrementGoal function.
Function overloading in C++
Function overloading means two or more functions can have the same name, but either the number of arguments or the data type of arguments has to be different. In the first example, we create two functions of the same name, one for adding two integers and another for adding two floats. In the second program, we make two functions with identical names but pass them a different number of arguments. Function overloading is also known as compile-time polymorphism.
Function overloading C++ program
#include <iostream>
#include<conio.h>
using namespace std;
/* Function arguments are of different data type */
int add(int, int);
float add(float, float);
float add(float, float);
int main()
{
int a, b, x;
float c, d, y;
{
int a, b, x;
float c, d, y;
cout << "Enter two integers\n";
cin >> a >> b;
cin >> a >> b;
x = add(a, b);
cout << "Sum of integers: " << x << endl;
cout << "Enter two floating point numbers\n";
cin >> c >> d;
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
return 0;
}
}
int add(int x, int y)
{
int sum;
{
int sum;
sum = x + y;
return sum;
}
}
float add(float x, float y)
{
float sum;
{
float sum;
sum = x + y;
return sum;
}
}
Scope resolution operator in C++
Scope resolution operator (::) in C++ is used to define a function outside a class or when we want to use a global variable but also has a local variable with the same name.
C++ programming code
#include <iostream>
using namespace std;
char c = 'a'; // global variable (accessible to all functions)
int main() {
char c = 'b'; // local variable (accessible only in main function)
cout << "Local variable: " << c << "\n";
cout << "Global variable: " << ::c << "\n"; // Using scope resolution operator
return 0;
}
char c = 'a'; // global variable (accessible to all functions)
int main() {
char c = 'b'; // local variable (accessible only in main function)
cout << "Local variable: " << c << "\n";
cout << "Global variable: " << ::c << "\n"; // Using scope resolution operator
return 0;
}
Output of program:
Local Variable: B
Global Variable : A
Scope resolution operator in class
#include <iostream>
using namespace std;
using namespace std;
class Game {
public:
void play(); // Function declaration
};
public:
void play(); // Function declaration
};
// function definition outside the class
void Game::play() {
cout << "Function defined outside the class.\n";
}
cout << "Function defined outside the class.\n";
}
int main() {
Game g;
g.play();
Game g;
g.play();
return 0;
}
}
Output of program:
Function Define Outside the class ;
C++ program for prime numbers
C++ program to print first n prime numbers.
C++ program
#include <iostream>
#include <cmath>
#include <cmath>
using namespace std;
int main()
{
int n, status = 1, num = 3, count, c;
{
int n, status = 1, num = 3, count, c;
cout << "Enter the number of prime numbers to print\n";
cin >> n;
cin >> n;
if (n >= 1)
{
cout << "First " << n <<" prime numbers are :-" << endl;
cout << 2 << endl;
}
{
cout << "First " << n <<" prime numbers are :-" << endl;
cout << 2 << endl;
}
for (count = 2; count <=n; )
{
for (c = 2; c <= (int)sqrt(num); c++)
{
if (num%c == 0)
{
status = 0;
break;
}
}
if (status != 0)
{
cout << num << endl;
count++;
}
status = 1;
num++;
}
return 0;
}
{
for (c = 2; c <= (int)sqrt(num); c++)
{
if (num%c == 0)
{
status = 0;
break;
}
}
if (status != 0)
{
cout << num << endl;
count++;
}
status = 1;
num++;
}
return 0;
}
0 Comments