C++ Tutorial
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
C++ is a high-level programming language that supports object-oriented, procedural, and generic programming paradigms. It was developed by Bjarne Stroustrup at Bell Labs in the late 1970s as an enhancement of the C programming language. The goal was to add features that would facilitate better program organization and the handling of larger software projects.
1. Key Features of C++
- Object-Oriented Programming (OOP): C++ provides support for encapsulation, inheritance, and polymorphism, enabling developers to create modular and reusable code.- Low-Level Manipulation: Like C, C++ allows direct manipulation of hardware and memory, making it suitable for system programming.
- Standard Template Library (STL): C++ includes a powerful library known as the Standard Template Library, which provides a collection of template classes and functions for common data structures and algorithms.
- Strongly Typed Language: C++ enforces strong type-checking, reducing the risk of errors during runtime.
- Compatibility with C: C++ maintains backward compatibility with C, allowing C programs to be compiled and run in C++ environments.
2. Basic Structure of a C++ Program
A typical C++ program consists of the following components:- Preprocessor Directives: These are commands that tell the compiler to include libraries or files before actual compilation begins.
- Namespace Declaration: C++ uses namespaces to organize code into logical groups and to prevent name collisions.
- Main Function: This is the entry point of every C++ program. The execution starts from this function.
- Statements: The code within the main function, which is executed sequentially.
Example Program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Output:
Hello, World!
3. Data Types in C++
C++ supports several built-in data types:- Basic Data Types:
- `int`: Integer type.
- `float`: Floating-point type for single-precision.
- `double`: Floating-point type for double-precision.
- `char`: Character type.
- Derived Data Types:
- Arrays
- Functions
- Pointers
- User-Defined Data Types:
- `struct`: Used to create a structure.
- `class`: Used to define a class in object-oriented programming.
Example of User-Defined Data Types:
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
};
int main() {
Student student1;
student1.name = "Alice";
student1.age = 20;
cout << "Name: " << student1.name << ", Age: " << student1.age << endl;
return 0;
}
Output:
Name: Alice, Age: 20
4. Control Structures
C++ includes various control structures to manage the flow of the program:- Conditional Statements:
- `if`, `else if`, `else`
- `switch`
- Loops:
- `for`, `while`, `do-while`
Example Program Using Control Structures:
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}
return 0;
}
Output:
Enter a number: 5
5 is odd.
5. Functions in C++
Functions are blocks of code designed to perform specific tasks. They can take parameters and return values.Function Prototype:
return_type function_name(parameter_list);
Example Program Using Functions:
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(10, 5);
cout << "Sum: " << result << endl;
return 0;
}
Output:
Sum: 15
6. Object-Oriented Programming (OOP) in C++
C++ supports the OOP paradigm, which allows for the creation of classes and objects:- Class: A blueprint for creating objects (a particular data structure).
- Object: An instance of a class.
Example of Class and Object:
#include <iostream>
using namespace std;
class Car {
public:
string brand;
string model;
int year;
void display() {
cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << endl;
}
};
int main() {
Car car1;
car1.brand = "Toyota";
car1.model = "Corolla";
car1.year = 2020;
car1.display();
return 0;
}
Output:
Brand: Toyota, Model: Corolla, Year: 2020
7. Inheritance
Inheritance allows a class to inherit attributes and methods from another class, promoting code reusability.Example of Inheritance:
#include <iostream>
using namespace std;
class Vehicle {
public:
void honk() {
cout << "Honk! Honk!" << endl;
}
};
class Car : public Vehicle {
public:
void display() {
cout << "I am a car." << endl;
}
};
int main() {
Car myCar;
myCar.honk();
myCar.display();
return 0;
}
Output:
Honk! Honk!
I am a car.
8. Polymorphism
Polymorphism allows methods to do different things based on the object that it is acting upon. It can be achieved through function overloading and operator overloading.Example of Polymorphism (Function Overloading):
#include <iostream>
using namespace std;
class Print {
public:
void show(int i) {
cout << "Integer: " << i << endl;
}
void show(double d) {
cout << "Double: " << d << endl;
}
};
int main() {
Print obj;
obj.show(5);
obj.show(5.5);
return 0;
}
Output:
Integer: 5
Double: 5.5
9. Templates
Templates allow functions and classes to operate with generic types, enabling code reusability.Example of Function Template:
#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << "Sum of integers: " << add(5, 10) << endl;
cout << "Sum of doubles: " << add(5.5, 2.5) << endl;
return 0;
}
Output:
Sum of integers: 15
Sum of doubles: 8.0
10. Exception Handling
Exception handling in C++ allows programmers to manage runtime errors effectively. The `try`, `catch`, and `throw` keywords are used for handling exceptions.Example of Exception Handling:
#include <iostream>
using namespace std;
int main() {
try {
int a = 5;
int b = 0;
if (b == 0) {
throw "Division by zero error!";
}
cout << "Result: " << a / b << endl;
} catch (const char* msg) {
cerr << "Error: " << msg << endl;
}
return 0;
}
Output:
Error: Division by zero error!
Conclusion
C++ is a powerful programming language that provides a vast range of features for different programming paradigms. Its ability to handle both low-level memory manipulation and high-level abstractions makes it suitable for a wide variety of applications, from system software to game development.