paint-brush
Re-learning the Basics of C++by@learncodingwithme
1,217 reads
1,217 reads

Re-learning the Basics of C++

by Akshay SharmaJune 7th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

C++ was originally intended to extend the C programming language with features that make object-oriented programming easier. It added features that allowed for modular programming, improved data abstractions, and generic programming. C++ is not fully backward compatible with C, but it is close enough that 95 percent of C programs should compile as C++. The main function is the main function that handles the program's execution. The basic mathematical operators are addition (+, subtraction (-), diversion (/), multiplication (*, and modulus (percent)

Company Mentioned

Mention Thumbnail
featured image - Re-learning the Basics of C++
Akshay Sharma HackerNoon profile picture

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++.

Header File

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

Operator types

1. Assignment Operator  

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.

2. Mathematical Operators

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. 

3. Relational Operators

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.

4. Logical Operators

AND (&&) and OR (||) are the logical operators. They are used to combine two distinct expressions. 

5. Bitwise Operators

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 

6. Shift Operators

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 - >>>

7. Unary Operators

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.

8. Tertiary Operator 

The ternary if-else?: is a three-operand operator. Example,

int x = 500;

x > 100 ? cout << "It is true" : cout << "It is false"

9. Comma Operator

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 declaration syntax

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.

Object creation

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.

"Hello, World!" C++ Program 

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!