0% found this document useful (0 votes)
14 views

C++ Programs Demonstration

OOP

Uploaded by

bbingandadi3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

C++ Programs Demonstration

OOP

Uploaded by

bbingandadi3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Table of Contents

1. C++ "Hello World!" Program ................................................................................................................. 2


2. C++ Comments ...................................................................................................................................... 2
Single Line Comments .......................................................................................................................... 3
3. C++ if...else Statement .......................................................................................................................... 3
4. Printing Numbers From 1 to 5 .............................................................................................................. 4
5. Display a text 5 times ............................................................................................................................ 4
6. Display Numbers from 1 to 5 ................................................................................................................ 5
7. C++ Function Prototype ........................................................................................................................ 5
8. Concatenate String Objects .................................................................................................................. 6
9. Take Inputs from User and Store Them in an Array ............................................................................. 7
10. Display Sum and Average of Array Elements Using for Loop ........................................................... 8
11. Print ASCII Value in C++..................................................................................................................... 9
12. Object and Class in C++ Programming .............................................................................................. 9
13. Using public and private in C++ Class.............................................................................................. 11
14. Working of C++ Pointers ................................................................................................................. 12
15. C++ Multilevel Inheritance .............................................................................................................. 13
16. Multiple Inheritance in C++ Programming...................................................................................... 13
17. Compute quotient and remainder .................................................................................................. 14
18. Find Largest Number Using if...else Statement .............................................................................. 15
19. Find the Factorial of a Given Number ............................................................................................. 16
20. Prefix Increment ++ operator overloading with return type .......................................................... 17
21. Fizz Buzz Implementation in C++ .................................................................................................... 18
22. To Create and Write To a File ......................................................................................................... 19
23. For more :........................................................................................................................................ 19

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.

There are two ways to add comments to code:

// - Single Line Comments


/* */ -Multi-line Comments

Page 2 of 19
Single Line Comments
In C++, any line that starts with // is a comment. For example,

// declaring a variable
int a;

// initializing the variable 'a' with the value 2


a = 2;

3. C++ if...else Statement


// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number

#include <iostream>
using namespace std;

int main() {

int number;

cout << "Enter an integer: ";


cin >> number;

if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}

cout << "This line is always printed.";

return 0;
}

Output 1

Page 3 of 19
Enter an integer: 4
You entered a positive integer: 4.
This line is always printed.

4. Printing Numbers From 1 to 5


#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}

Output

1 2 3 4 5

5. Display a text 5 times


// C++ Program to display a text 5 times

#include <iostream>

using namespace std;

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!

6. Display Numbers from 1 to 5


// C++ Program to print numbers from 1 to 5

#include <iostream>

using namespace std;

int main() {
int i = 1;

// while loop from 1 to 5


while (i <= 5) {
cout << i << " ";
++i;
}

return 0;
}

Output

1 2 3 4 5

7. C++ Function Prototype


// using function definition after main() function
// function prototype is declared before main()

#include <iostream>

using namespace std;

// function prototype
int add(int, int);

Page 5 of 19
int main() {
int sum;

// calling the function and storing


// the returned value in sum
sum = add(100, 78);

cout << "100 + 78 = " << sum << endl;

return 0;
}

// function definition
int add(int a, int b) {
return (a + b);
}

Output

100 + 78 = 178

8. Concatenate String Objects


#include <iostream>
using namespace std;

int main()
{
string s1, s2, result;

cout << "Enter string s1: ";


getline (cin, s1);

cout << "Enter string s2: ";


getline (cin, s2);

result = s1 + s2;

cout << "Resultant String = "<< result;

Page 6 of 19
return 0;
}

Output

Enter string s1: C++ Programming


Enter string s2: is awesome.
Resultant String = C++ Programming is awesome.

9. Take Inputs from User and Store Them in


an Array
#include <iostream>
using namespace std;

int main() {

int numbers[5];

cout << "Enter 5 numbers: " << endl;

// store input from user to array


for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}

cout << "The numbers are: ";

// print array elements


for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}

return 0;
}

Output

Enter 5 numbers:

Page 7 of 19
11
12
13
14
15
The numbers are: 11 12 13 14 15

10. Display Sum and Average of Array


Elements Using for Loop
#include <iostream>
using namespace std;

int main() {

// initialize an array without specifying size


double numbers[] = {7, 5, 6, 12, 35, 27};

double sum = 0;
double count = 0;
double average;

cout << "The numbers are: ";

// print array elements


// use of range-based for loop
for (const double &n : numbers) {
cout << n << " ";

// calculate the sum


sum += n;

// count the no. of array elements


++count;
}

// print the sum


cout << "\nTheir Sum = " << sum << endl;

Page 8 of 19
// find the average
average = sum / count;
cout << "Their Average = " << average << endl;

return 0;
}

Output

The numbers are: 7 5 6 12 35 27


Their Sum = 92
Their Average = 15.3333

11. Print ASCII Value in C++


#include <iostream>
using namespace std;

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

12. Object and Class in C++ Programming


// Program to illustrate the working of
// objects and class in C++ Programming

#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() {

// create object of Room class


Room room1;

// assign values to data members


room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

// calculate and display the area and volume of the room


cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;

return 0;
}

Output

Area of Room = 1309


Volume of Room = 25132.8

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:

// function to initialize private variables


void initData(double len, double brth, double hgt) {
length = len;
breadth = brth;
height = hgt;
}

double calculateArea() {
return length * breadth;
}

double calculateVolume() {
return length * breadth * height;
}
};

int main() {

// create object of Room class


Room room1;

// pass the values of private variables as arguments


room1.initData(42.5, 30.8, 19.2);

cout << "Area of Room = " << room1.calculateArea() << endl;


cout << "Volume of Room = " << room1.calculateVolume() << endl;

Page 11 of 19
return 0;
}

Output

Area of Room = 1309


Volume of Room = 25132.8

14. Working of C++ Pointers


#include <iostream>
using namespace std;
int main() {
int var = 5;

// declare pointer variable


int* pointVar;

// store address of var


pointVar = &var;

// print value of var


cout << "var = " << var << endl;

// print address of var


cout << "Address of var (&var) = " << &var << endl
<< endl;

// print pointer pointVar


cout << "pointVar = " << pointVar << endl;

// print the content of the address pointVar points to


cout << "Content of the address pointed to by pointVar (*pointVar) = " <<
*pointVar << endl;

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

15. C++ Multilevel Inheritance


#include <iostream>
using namespace std;

class A {
public:
void display() {
cout<<"Base class content.";
}
};

class B : public A {};

class C : public B {};

int main() {
C obj;
obj.display();
return 0;
}

Output

Base class content.

16. Multiple Inheritance in C++ Programming


#include <iostream>

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;
}
};

class Bat: public Mammal, public WingedAnimal {};

int main() {
Bat b1;
return 0;
}

Output

Mammals can give direct birth.


Winged animal can flap.

17. Compute quotient and remainder


#include <iostream>
using namespace std;

int main()
{
int divisor, dividend, quotient, remainder;

cout << "Enter dividend: ";


cin >> dividend;

cout << "Enter divisor: ";

Page 14 of 19
cin >> divisor;

quotient = dividend / divisor;


remainder = dividend % divisor;

cout << "Quotient = " << quotient << endl;


cout << "Remainder = " << remainder;

return 0;
}

Output

Enter dividend: 13
Enter divisor: 4
Quotient = 3
Remainder = 1

18. Find Largest Number Using if...else


Statement
#include <iostream>
using namespace std;

int main() {
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if((n1 >= n2) && (n1 >= n3))


cout << "Largest number: " << n1;
else if ((n2 >= n1) && (n2 >= n3))
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;

return 0;
}

Page 15 of 19
Output

Enter three numbers: 2.3


8.3
-4.2
Largest number: 8.3

19. Find the Factorial of a Given Number


#include <iostream>
using namespace std;

int main() {
int n;
long factorial = 1.0;

cout << "Enter a positive integer: ";


cin >> n;

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

Enter a positive integer: 4


Factorial of 4 = 24

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 type is Check


Check operator ++()
{
Check temp;
++i;
temp.i = i;

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

21. Fizz Buzz Implementation in C++


#include <iostream>
using namespace std;

int main(){

for (int i=1; i<=100; i++){

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");

// Write to the file


MyFile << "Files can be tricky, but it is fun enough!";

// Close the file


MyFile.close();
}

23. For more :


visit the following recommended sites:

https://www.w3schools.com/cpp/default.asp

https://www.tutorialspoint.com/cplusplus/index.htm

https://www.programiz.com/cpp-programming

Page 19 of 19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy