C++ Tutorial
c plus training in vizag
C++ tutorial provides basic and advanced concepts of C++. Our C++ tutorial is designed for beginners and professionals.
Programming in C++ follows an object-oriented design. It is a C programming extension.
Everything from the first example to control statements, objects and classes, inheritance, constructors and destructors, this, static, polymorphism, abstraction, abstract class, interface, namespace, encapsulation, arrays, strings, exception handling, File IO, and more is covered in this comprehensive C++ course.
c plus training in vizag
What is C++
Object-oriented, procedural, and generic programming are all supported by the general-purpose, case-sensitive, free-form programming language C++.
C++ is a middle-level language, as it encapsulates both high and low level language features.
Object-Oriented Programming (OOPs)
The four main pillars of object-oriented programming (OOPs), which are supported by C++, are as follows:
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
C++ Standard Libraries
Standard C++ programming is divided into three important parts:
- The core library includes the data types, variables and literals, etc.
- The standard library includes the set of functions manipulating strings, files, etc.
- The Standard Template Library (STL) includes the set of methods manipulating a data structure.
c plus training in vizag
Usage of C++
The C++ programming language allows us to create a variety of reliable and safe programs, including:
- Window application
- Client-Server application
- Device drivers
- Embedded firmware etc
C++ Program
In this tutorial, all C++ programs are given with C++ compiler so that you can easily change the C++ program code.
File: main.cpp
- #include <iostream>
- using namespace std;
- int main() {
- cout << “Hello C++ Programming”;
- return 0;
- }
C++ Control Statement
C++ if-else
In C++ programming, if statement is used to test the condition. There are various types of if statements in C++.
- if statement
- if-else statement
- nested if statement
- if-else-if ladder
C++ IF Statement
The C++ if statement tests the condition. It is executed if condition is true.
- if(condition){
- //code to be executed
- }
C++ If Example
- #include <iostream>
- using namespace std;
- int main () {
- int num = 10;
- if (num % 2 == 0)
- {
- cout<<“It is even number”;
- }
- return 0;
- }
Output:/p>
It is even number
C++ IF-else Statement
The C++ if-else statement also tests the condition. It executes if block if condition is true otherwise else block is executed.
- if(condition){
- //code if condition is true
- }else{
- //code if condition is false
- }
C++ If-else Example
- #include <iostream>
- using namespace std;
- int main () {
- int num = 11;
- if (num % 2 == 0)
- {
- cout<<“It is even number”;
- }
- else
- {
- cout<<“It is odd number”;
- }
- return 0;
- }
Output:
It is odd number
C++ If-else Example: with input from user
- #include <iostream>
- using namespace std;
- int main () {
- int num;
- cout<<“Enter a Number: “;
- cin>>num;
- if (num % 2 == 0)
- {
- cout<<“It is even number”<<endl;
- }
- else
- {
- cout<<“It is odd number”<<endl;
- }
- return 0;
- }
Output:
Enter a number:11 It is odd number
Output:
Enter a number:12 It is even number
c plus training in vizag
C++ IF-else-if ladder Statement
The C++ if-else-if ladder statement executes one condition from multiple statements.
- if(condition1){
- //code to be executed if condition1 is true
- }else if(condition2){
- //code to be executed if condition2 is true
- }
- else if(condition3){
- //code to be executed if condition3 is true
- }
- …
- else{
- //code to be executed if all the conditions are false
- }
C++ If else-if Example
- #include <iostream>
- using namespace std;
- int main () {
- int num;
- cout<<“Enter a number to check grade:”;
- cin>>num;
- if (num <0 || num >100)
- {
- cout<<“wrong number”;
- }
- else if(num >= 0 && num < 50){
- cout<<“Fail”;
- }
- else if (num >= 50 && num < 60)
- {
- cout<<“D Grade”;
- }
- else if (num >= 60 && num < 70)
- {
- cout<<“C Grade”;
- }
- else if (num >= 70 && num < 80)
- {
- cout<<“B Grade”;
- }
- else if (num >= 80 && num < 90)
- {
- cout<<“A Grade”;
- }
- else if (num >= 90 && num <= 100)
- {
- cout<<“A+ Grade”;
- }
- }
Output:
Enter a number to check grade:66 C Grade
Output:
Enter a number to check grade:-2 wrong number