Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

C++ Basic to Intermediate Level NotesIntroduction

C++ is a general-purpose programming language that is widely used in a variety of applications,


including operating systems, device drivers, games, and embedded systems. It is a powerful
and versatile language, but it can also be complex and challenging to learn.

This document provides a basic overview of C++, including the following topics:

● Variables and data types


● Operators and expressions
● Control flow statements
● Functions
● Classes and objects
● Pointers and references
● Memory management
● Templates
● Standard library

Variables and data types

A variable is a named storage location that can hold a value. C++ has a variety of data types,
including integers, floating-point numbers, characters, and strings.

To declare a variable, you must specify its data type and its name. For example, the following
code declares an integer variable named age:

C++
int age;

You can then assign a value to the variable using the assignment operator (=). For example, the
following code assigns the value 25 to the variable age:

C++
age = 25;

Operators and expressions

Operators are used to perform operations on data. C++ has a variety of operators, including
arithmetic operators, relational operators, logical operators, and bitwise operators.

An expression is a combination of operands and operators. Expressions can be used to


evaluate values and to control the flow of a program.

For example, the following expression evaluates the sum of the variables age and 1:

C++
age + 1

The following expression compares the values of the variables age and 18:

C++
age > 18

Control flow statements

Control flow statements are used to control the flow of a program. C++ has a variety of control
flow statements, including the if statement, the else statement, the while loop, and the for loop.

The if statement is used to execute a block of code if a condition is true. The else statement is
used to execute a block of code if a condition is false.

The while loop is used to execute a block of code while a condition is true. The for loop is used
to execute a block of code a specified number of times.

For example, the following code uses an if statement to check if the variable age is greater than
18. If it is, the code prints a message to the console:

C++
if (age > 18) {
cout << "You are an adult." << endl;
}

Functions

A function is a block of code that can be reused throughout a program. Functions can take
parameters and can return values.

To define a function, you must specify its return type, its name, and its parameters. For example,
the following code defines a function named add(), which takes two integer parameters and
returns an integer value:

C++
int add(int a, int b) {
return a + b;
}

To call a function, you simply need to specify its name and its arguments. For example, the
following code calls the add() function and prints the result to the console:

C++
int result = add(1, 2);
cout << result << endl;

Classes and objects

A class is a blueprint for creating objects. Objects are self-contained entities that contain data
and code.

To define a class, you must specify its name and its members. Class members can be variables,
functions, or other classes.

For example, the following code defines a class named Person, which has two members: a
name variable and a greet() function:

C++
class Person {
public:
string name;

void greet() {
cout << "Hello, my name is " << name << endl;
}
};
To create an object from a class, you use the new keyword. For example, the following code
creates a Person object and assigns it to the variable person:

C++
Person *person = new Person();

You can then access the object's members using the dot operator (.). For example, the following
code sets the object's name and then calls its greet() function:

C++
person->name = "John Doe";
person->greet();

Pointers and references

A pointer is a variable that stores the address of another variable. References are similar to
pointers, but they are more strongly typed.

You might also like