C++ programming examples

C++ programming examples

C++ programming examples

C++ programming examples

C++ programming examples

C++ programming examples

WhatsApp
LinkedIn
Pinterest

C++ programming examples

C++ Programming Examples

What is c++?

C++ is a versatile computer programming and object oriented programming language. It can perform simple and difficult tasks. 

Its major purpose was building computers and large applications. Quick, useful, and adaptable are all things it does well.

People mainly use C++ for all sorts of projects, like making computer programs, servers for websites, and even things that need to work really, really well, like the technology in space probes.

When you write C++ code, you need a special program to turn it into something your computer can understand. This works on many types of computers, and lots of companies make these special programs.

C++ (“cee plus plus”) is like an upgraded version of the C language. It supports functional and procedural programming, object-oriented programming, and templates. 

Thus, C++ is a great tool for designing software that runs on many devices and performs many tasks.

The best and most effective way to learn C++ or any programming language is by practicing examples. So in this blog will explore some C++ programming examples so you get a clear understanding of C++ programming language.


Enroll now and take the first step towards a successful career. Click here to join our courses today!


Basic c++ programs

  1. C++ program for hello world
  2. Addition of two numbers in c++
  3. Swap two numbers in c++
  4. Write a program call by reference in c++
  5. Write a c++ program for factorial of a number
  6. Fibonacci series program in c++
  7. How to find gcd of two numbers in c++ program 
  8. W.A.P using constructor in c++
  9. Write a program this pointer in c++

Level up your skills in Programming. Enroll now and become an expert. 


C++ program for hello world

 

Explanation:

  • In C++, #include <iostream> is a preprocessor directive that tells the compiler to include the program’s input/output stream library. 
  • In C++ the program execution begins in the main() function. 
  • In this Hello world c++ programming example, the main() function uses std::cout to output the message “Hello, World!” to the console. 
  • The “return 0” statement shows that the program was successful in functioning.
  • The phrase “hello world program in C++” will appear on your console when you start this program. 

Many people learn this “Hello World Program” , a common first program when learning a new programming language. 


Addition of two numbers in c++ programming examples

Here’s our explanation of the additional program in C++:

  • We start by including the iostream library, which provides input and output capabilities.
  • The three integer variables num1, num2, and sum are declared.
  • We use cout (pronounced “see-out”) to prompt the user to enter the first number. Then we use cin (pronounced “see-in”) to read the user’s input & store it in num1.
  • We do the same thing to prompt for and read in the second number, storing it in num2.
  • Adding num1 and num2 together and storing the result in sum.
  • We use cout to print out the result and a message indicating that it is the sum.
  • Finally, returning 0 indicates that the program has run successfully.

Swapping of two numbers in c++ programming examples

A number swap means changing two variables. 

We can swap numbers in C++ using two methods – 

  1. Using a temp Variable
  2. Without Using temp Variable (Using Arithmetic Operations)

Swap Two Numbers Using Temp Variable

Here’s explanation of swap two numbers using temp variable in C++:

  • Include <iostream> for input and output.
  • Declare num1, num2, and temp for two numbers and a temporary variable.
  • Get user input for num1 and num2.
  • Use temp to swap num1 and num2 values.
  • Display swapped values of num1 and num2.
  • Terminate the program with return 0;

Swap Two Numbers Without Using Temp Variable 

  • Include <iostream> for input and output.
  • Declare num1 and num2 for two numbers.
  • Get user input for num1 and num2.
  • Swap num1 and num2 values without using a temporary variable using arithmetic operations.
  • Display the swapped values of num1 and num2.
  • Terminate the program with return 0;.

What is call by value and call by reference in c++

In C++ there are two ways to passed argument to a function – 

  1. Call by value
  2. Call by reference

 

1) Call by value: In this method when arguments are passed by value, a copy of the argument is made and sent to the method. 

This means that whatever changes made to the argument inside the method will only affect the copy and not the original value. The original number stays the same after the function is done.

 

2) Call by reference: In this method when arguments are passed via reference, the function is given a reference to the argument instead of a copy of it. 

This means that any changes made to the argument inside the code are applied to the original value, not a copy of it.

Write a program call by reference in c++

Explanation:

  • The program demonstrates call-by-reference in C++.
  • It defines a function called swap that takes two integer parameters by reference using the & operator.
  • Inside the swap function, the values of the parameters are swapped using a temporary variable.
  • The program defines two integer variables called x and y in the main function and initializes them to 5 and 10, respectively.
  • The program then calls the swap function, passing in x and y as arguments.
  • Because x and y are passed by reference, the swap function can modify their values directly in memory.
  • Following the function call, the x and y will be 10 and 5, respectively.
  • The function should declare the parameter as a reference type to pass a variable by reference using the & operator. The calling code should also pass the variable with the & operator. You can ensure that the function gets the variable’s memory address rather than a copy of its value.

Factorial program in c++

In a C++ programming example, factorial is the product of all positive integers up to a given number. The factorial of 7 is 7 × 6 x 5 x 4 x 3 x 2 x 1, for instance, which equals 5040.

 

Write a c++ program for factorial of a number

Explanation:

  • Include the <iostream> library for input/output and use using namespace std;.
  • Define a recursive function factorial to calculate the factorial of a number.
  • The function checks if the number is 0; if so, it returns 1 (base case).
  • If the number is not 0, it uses recursion to calculate the factorial.
  • In the main function, get user input for a positive integer.
  • Calculate and display the factorial using the factorial function.
  • Terminate the program with return 0;.

Fibonacci series examples in c++

C++ fibonacci series example are 0, 1, 1, 2, 3, 5, 8, 13, and 21.

Fibonacci series program in c++ programming examples

Explanation: 

  • The user is asked how many terms in the Fibonacci series they want to produce.
  • The program uses a loop to generate the series.
  • The loop starts with t1 = 0 & t2 = 1, the first two terms in the series.
  • Adding the two terms (t1 and t2) before it in the loop.
  • The loop continues until it reaches the desired number of terms.
  • Each term is outputted to the console using the cout function.

Find GCD in C++ programming language 

The largest positive integer that divides two or more numbers without leaving a residual is known as the “greatest common divisor,” or GCD. 

The Euclidean algorithm can be used in C++ programming to find the GCD of two numbers. One important idea behind the Euclidean method is that the greatest common divisor (GCD) of any two numbers stays the same.

Suppose the larger number is replaced by the difference between it & the smaller number. This fact forms the basis for the algorithm’s iterative approach to finding the GCD.

How to find gcd of two numbers in c++ program 

Explanation:

  • Include the <iostream> library for input/output and use using namespace std;.
  • Define a recursive function gcd to calculate the greatest common divisor (GCD) of two integers.
  • The function uses a base case: if one of the numbers is 0, it returns the other number.
  • If not, it uses recursion by calling itself with the arguments (b, a % b), where a % b represents the remainder.
  • In the main function, get user input for two positive integers.
  • Calculate and display the GCD using the gcd function.
  • Terminate the program with return 0;.

Benefits of oop in c++

  • Modularity
  • Reusability
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Simplicity and Clarity
  • Effective Problem Solving

What is a constructor?

A function called constructor has a name that is similar to the class name. Constructors are generally used to initialize class members(member variables). Class variables (Instance vars) can’t be initialized when declared. Constructors get called automatically at the time of object creation.

Two types of constructors:

  1. Constructor without argument (zero-argument constructor) 
  2. Constructor with arguments (parameterized constructor)

 

  • The class name and the constructor’s name are similar.
  • Every time an object of the related class is created, the constructor is called.
  • They should be declared in the public section.
  • They do not have a return type(not even void)
  • They cannot be inherited
  • They can have default arguments.
  • They cannot be virtual.
  • We cannot refer to their addresses.

 

Constructor in c++ programming example:

Explanation:

  • Include the <iostream> library for input/output and use using namespace std;.
  • Define a Rectangle class with private data members width and height.
  • The class has a constructor that sets the width and height when a Rectangle object is created.
  • The Rectangle class also contains a public area method to calculate and return the area of the rectangle.
  • In the main function, create a Rectangle object named rect with dimensions 5×10.
  • Calculate and display the area of the rectangle using the area method.
  • Terminate the program with return 0;

What is this pointer in c++?

Member functions of every object have access to a pointer named ‘this’, which points to the object itself. Sometimes, it may happen that instance_var and local variables have the same name.

It is an implicit parameter that all member function calls get, and it can be used to get to the data members and member functions of the current object.

Sometimes, when a member method parameter has the same name as a class data member, the “this” pointer can be useful. In this case, the “this” pointer helps distinguish between these two values.

 

Write a program this pointer in c++

Explanation:

  • Include the <iostream> library for input/output and use using namespace std;.
  • Define a MyClass class with a private data member x.
  • The class has a setX method that sets the class’s x using the “this” pointer to distinguish it from the method parameter.
  • It also has a getX method to retrieve the class’s x value, again using the “this” pointer.
  • In the main function, create a MyClass object named obj.
  • Set the value of x using the setX method.
  • Retrieve and display the value of x using the getX method.
  • Terminate the program with return 0;.

Conclusion:

I hope you get a better understanding of c++ programming examples and for sure that can help you in your coding career. If you’re a fresher, practicing these examples is the first step to learning C++.

You can use C++ for a lot of different things, from making games to building AI. These C++ programming examples will help you get started. 

So, keep coding, keep practicing, and keep learning. 

Contact Us +91 8600998107/ +91 7028710777 For any Queries.

Aniket

Aniket

Leave a Reply

Your email address will not be published. Required fields are marked *

Blogs You May Like

Get in touch to claim Best Available Discounts.

If You Are Looking for Job Assistance Please Fill Up the Form.

× How can I help you?