C++ is an extension of the C programming language with additional features like object-oriented programming (OOP). Below are some fundamental C++ concepts and syntax:
#include
The #include
directive is used to include standard or user-defined libraries and header files in a C++ program.
#include <iostream> // Includes the Input-Output stream library
#include <string> // Includes the String library for string manipulations
main
The main
function is the entry point of a C++ program. The execution starts from this function.
int main() {
// Your code here
return 0; // Returns 0 to indicate successful execution
}
cout
and cin
The cout
object is used for outputting data to the console, while cin
is used for inputting data from the console.
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl; // Prints "Hello, World!" followed by a newline
int number;
std::cin >> number; // Takes an integer input from the user
return 0;
}
Variables
Variables in C++ store data values and must be declared with a type. Here are some common types:
int age = 25; // Integer variable with value 25
double height = 5.9; // Double variable for floating-point numbers
char grade = 'A'; // Character variable with value 'A'
std::string name = "Alice"; // String variable to store text
Control Structures
C++ control structures manage the flow of the program. They include conditional statements and loops:
if (age > 18) {
std::cout << "Adult" << std::endl; // Executes if age is greater than 18
} else {
std::cout << "Minor" << std::endl; // Executes if age is 18 or less
}
for (int i = 0; i < 5; i++) {
std::cout << i << std::endl; // Executes the block of code 5 times
}
while (height < 6.0) {
height += 0.1; // Repeats the block of code while height is less than 6.0
}
Functions
Functions in C++ are blocks of code designed to perform specific tasks. They help in code organization and reusability.
int add(int a, int b) {
return a + b; // Returns the sum of a and b
}
int result = add(5, 3); // Calls the add function with arguments 5 and 3
Classes and Objects
C++ is an object-oriented language, meaning it supports classes and objects. Classes are user-defined data types that encapsulate data and functions.
#include <iostream>
class Person {
public:
std::string name;
int age;
void introduce() {
std::cout << "My name is " << name << " and I am " << age << " years old." << std::endl;
}
};
int main() {
Person person1;
person1.name = "Alice";
person1.age = 30;
person1.introduce(); // Outputs: My name is Alice and I am 30 years old.
return 0;
}
Constructors and Destructors
Constructors initialize objects when they are created, while destructors clean up when objects are destroyed.
class Car {
public:
std::string model;
Car(std::string m) : model(m) { // Constructor
std::cout << "Car model " << model << " created." << std::endl;
}
~Car() { // Destructor
std::cout << "Car model " << model << " destroyed." << std::endl;
}
};
int main() {
Car car1("Toyota"); // Creates a Car object with model "Toyota"
return 0;
}
Inheritance
Inheritance allows a class to inherit attributes and methods from another class, facilitating code reusability.
class Animal {
public:
void eat() {
std::cout << "Eating..." << std::endl;
}
};
class Dog : public Animal {
public:
void bark() {
std::cout << "Woof!" << std::endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited method
myDog.bark(); // Method specific to Dog class
return 0;
}
Polymorphism
Polymorphism allows methods to do different things based on the object that is invoking them. It can be achieved using function overloading or function overriding.
#include <iostream>
class Base {
public:
virtual void show() { // Virtual function for dynamic binding
std::cout << "Base class show function" << std::endl;
}
};
class Derived : public Base {
public:
void show() override { // Overridden function
std::cout << "Derived class show function" << std::endl;
}
};
int main() {
Base* bptr;
Derived d;
bptr = &d;
bptr->show(); // Calls the Derived class version of show()
return 0;
}
Templates
Templates enable generic programming by allowing functions and classes to operate with any data type.
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
int main() {
std::cout << max(10, 5) << std::endl; // Calls max with integers
std::cout << max(3.5, 2.5) << std::endl; // Calls max with doubles
return 0;
}
Standard Template Library (STL)
The STL provides various template classes and functions such as vectors, lists, and maps for efficient data manipulation.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
std::cout << num << std::endl; // Outputs each number in the vector
}
return 0;
}
I think you are here to get to know me.
🌐 Connect with me across platforms.
🤝 Hit me up on these links, and let's turn ideas into action!