C++ was initially intended to extend the C programming language with features that make object-oriented programming easier. Finally, it added features that allowed for modular programming, basics of c++ improved data abstractions, and generic programming. C++ was eventually standardised as ISO/IEC 14882 in late 1998. (current version is 2003). C++ is not fully backward compatible with C, but it is close enough that 95 percent of C programs should compile quite easily as C++.
Similar to a C program, header files are included at the start. Header files contained pre-declared function libraries that users could use at their leisure. iostream is a header file that has input and output streams. It is denoted as
< #include iostream.h>.
Main Function
main() is the function that handles the program's execution. The main function's return type is int.
Cout
cout<< Whatever you will write here will appear on the screen.;
Comment
Single line comment
// is used before mentioning the comment in single-line comments. For example,
cout<<"hello"; / This is a single-line comment
Multiple line comment
Enclose the comment between /* and */ for multiple line comments. As an example,
/*this is a multi-line comment............. The comment comes to a conclusion */
C++ Data Types
Data types can either be built-in or abstract.
Basic Built-types
1. char
2. int
3. float
4. double
The assignment operator '=' takes the right-hand side (called rvalue) and copies it into the left-hand side (called lvalue). The assignment operator is the only one that can be overloaded but not inherited.
Basic mathematical operations are performed using operators.
The basic mathematical operators are addition (+), subtraction (-), diversion (/), multiplication (*), and modulus (percent). The modulus operator is incompatible with floating-point numbers.
These operators create a connection between operands. Less than (<), greater than (>), equal to (=), greater than equal to (>=), less than equal to (<=), equivalent (==), and not equivalent (!=) are the relational operators.
AND (&&) and OR (||) are the logical operators. They are used to combine two distinct expressions.
They're used to convert individual bits into numbers. They only work with integral data types such as char, int, and long, not with floating-point values.
=> & - Bitwise AND operator
=> | - Bitwise OR operator
=> ^ - XOR bitwise operator
=> ~ - NOT bitwise operator
Shift Operators are used to change the order of bits in any variable. There are three kinds of it.
Left Shift Operator - <<
Right Shift Operator - >>
Unsigned Right Shift Operator - >>>
These are the operators that only work on one operand. There are numerous unary operators, but the increment ++ and decrement - - operators are the most commonly used.
The ternary if-else?: is a three-operand operator. Example,
int x = 500;
x > 100 ? cout << "It is true" : cout << "It is false"
This is used to distinguish between variable names and expressions. memory set aside for data types, objects, and user-defined data types
C++ PROGRAM STRUCTURE
C++ programs are divided into four major sections:
1. Included header files
2. Class declarations
3. Member function definitions
4. Initialise main function purpose
The sections can be placed as separate code files that can be compiled together or independently.
Because class declarations and member function definitions are separated, the programmer can separate the interface's abstract specification from the implementation details.
The Main function includes all of the files required for execution in all sections. The class definitions and member functions act as a server, providing services to the client, which is the main program. The class's Public Interface is used for communication between the program's client and server elements.
class nameofclass {
private :
variable declarations;
variable declarations; //As many you want
function declarations;
function declarations; //As many you want
public :
function declarations;
function declarations; //As many you want
function declarations;
function declarations; //As many you want
};
=> Objects are instances of a class that hold the data variables declared in the class, and member functions operate on these class objects.
=> Classes contain member functions and data members, and how these data members and variables are accessed is determined by the access specifiers.
=> A class's member functions can be defined either within or outside the class definition.
=> A class has private access control by default.
=> During the class definition process, no storage is assigned.
=> The name of the class must begin with an uppercase letter.
Access specifiers
=> Access specifiers are used to limit the availability of class members.
=> In c programmming, access specifiers are separated by a colon.
=> In the same class, one, two, or all three specifiers can be used to set different boundaries for different class members.
Public: Everyone will be able to see all of the class members who have declared themselves as public. Other classes can use the said public data members and member functions.
Private: Outside of that class, no one has access to the class members who have been declared private. A compile-time error will occur if you attempt to access the private member. Class variables and member functions are by default private.
Protected: Class members outside the class are inaccessible, but any subclass of that class can access them. If class A inherits class B, then class B, a subclass of class A, has access.
After defining a class, any number of objects belonging to that class can be created. The type class variable is objects. A class is a grouping of objects of the same type. The object declaration allocates the necessary memory space for the object. The following class, for example, has three objects: obj1, obj2, and obj3.
It is an elementary program. It is frequently used to introduce a new programming language to a beginner.
// Hello world C++ Program
#include <iostream>
int main() {
std::cout << "This is my first program = Hello World!";
return 0;
}
Output
This is my first program = Hello World!