C++ Programs Demonstration
C++ Programs Demonstration
Page 1 of 19
1. C++ "Hello World!" Program
// Your First C++ Program
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Output
Hello World!
2. C++ Comments
In this tutorial, we will learn about C++ comments, why we use them, and how
to use them with the help of examples.
C++ comments are hints that a programmer can add to make their code
easier to read and understand. They are completely ignored by C++
compilers.
Page 2 of 19
Single Line Comments
In C++, any line that starts with // is a comment. For example,
// declaring a variable
int a;
#include <iostream>
using namespace std;
int main() {
int number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
return 0;
}
Output 1
Page 3 of 19
Enter an integer: 4
You entered a positive integer: 4.
This line is always printed.
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}
Output
1 2 3 4 5
#include <iostream>
int main() {
for (int i = 1; i <= 5; ++i) {
cout << "Hello World! " << endl;
}
return 0;
}
Output
Hello World!
Hello World!
Page 4 of 19
Hello World!
Hello World!
Hello World!
#include <iostream>
int main() {
int i = 1;
return 0;
}
Output
1 2 3 4 5
#include <iostream>
// function prototype
int add(int, int);
Page 5 of 19
int main() {
int sum;
return 0;
}
// function definition
int add(int a, int b) {
return (a + b);
}
Output
100 + 78 = 178
int main()
{
string s1, s2, result;
result = s1 + s2;
Page 6 of 19
return 0;
}
Output
int main() {
int numbers[5];
return 0;
}
Output
Enter 5 numbers:
Page 7 of 19
11
12
13
14
15
The numbers are: 11 12 13 14 15
int main() {
double sum = 0;
double count = 0;
double average;
Page 8 of 19
// find the average
average = sum / count;
cout << "Their Average = " << average << endl;
return 0;
}
Output
int main() {
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c);
return 0;
}
Output
Enter a character: p
ASCII Value of p is 112
#include <iostream>
Page 9 of 19
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
return 0;
}
Output
Page 10 of 19
13. Using public and private in C++ Class
// Program to illustrate the working of
// public and private in C++ Class
#include <iostream>
using namespace std;
class Room {
private:
double length;
double breadth;
double height;
public:
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
Page 11 of 19
return 0;
}
Output
return 0;
}
Page 12 of 19
Output
var = 5
Address of var (&var) = 0x61ff08
pointVar = 0x61ff08
Content of the address pointed to by pointVar (*pointVar) = 5
class A {
public:
void display() {
cout<<"Base class content.";
}
};
int main() {
C obj;
obj.display();
return 0;
}
Output
Page 13 of 19
using namespace std;
class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth." << endl;
}
};
class WingedAnimal {
public:
WingedAnimal() {
cout << "Winged animal can flap." << endl;
}
};
int main() {
Bat b1;
return 0;
}
Output
int main()
{
int divisor, dividend, quotient, remainder;
Page 14 of 19
cin >> divisor;
return 0;
}
Output
Enter dividend: 13
Enter divisor: 4
Quotient = 3
Remainder = 1
int main() {
float n1, n2, n3;
return 0;
}
Page 15 of 19
Output
int main() {
int n;
long factorial = 1.0;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
return 0;
}
Output
Page 16 of 19
20. Prefix Increment ++ operator overloading
with return type
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(0) { }
return temp;
}
void Display()
{ cout << "i = " << i << endl; }
};
int main()
{
Check obj, obj1;
obj.Display();
obj1.Display();
obj1 = ++obj;
obj.Display();
obj1.Display();
return 0;
}
Page 17 of 19
Output
i = 0
i = 0
i = 1
i = 1
int main(){
if (i%15 == 0)
cout<<"Fizz Buzz,\t";
else if ((i%3) == 0)
cout<<"Fizz,\t";
else if ((i%5) == 0)
cout<<"Buzz,\t";
else
cout<<i<<",\t";
}
return 0;
}
Output
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz,
22,
23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz,
43,
44, Fizz Buzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, Fizz Buzz, 61, 62,
Fizz,
64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, Fizz Buzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83,
Fizz,
Buzz, 86, Fizz, 88, 89, Fizz Buzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz,
Page 18 of 19
22. To Create and Write To a File
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");
https://www.w3schools.com/cpp/default.asp
https://www.tutorialspoint.com/cplusplus/index.htm
https://www.programiz.com/cpp-programming
Page 19 of 19