/All-About-CPP-Langauage-Coding-

This repo includes all the essential things for CPP by coding that everyone should know about that.

Primary LanguageC++

All About C++ Langauage(Coding)

This repo includes all the essential things with coding as well as theory in README file..... that everyone should know about that.

C++ is a cross-platform language that can be used to create high-performance applications. C++ was developed by Bjarne Stroustrup, as an extension to the C language. C++ gives programmers a high level of control over system resources and memory.

1.ENUM
Enumeration is a user defined datatype in C/C++ language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration.

2.Typedef
The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.

3.Conditional Statement
Conditional statements, also known as selection statements, are used to make decisions based on a given condition. If the condition evaluates to True, a set of statements is executed, otherwise another set of statements is executed.
The if Statement:
The if statement selects and executes the statement(s) based on a given condition. If the condition evaluates to True then a given set of statement(s) is executed. However, if the condition evaluates to False, then the given set of statements is skipped and the program control passes to the statement following the if statement. The syntax of the if statement is if (condition) { statement 1; statement 2; statement 3; } else {

} 4.Nested Conditonal Statement
A nested if in C is an if statement that is the target of another if statement. Nested if statements means an if statement inside another if statement. Yes, both C and C++ allows us to nested if statements within if statements, i.e, we can place an if statement inside another if statement
if{
statement:
if{
statement:
if{
statement:
}
}
}
else
{
}

5.Switch Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
switch(expression) {

case constant-expression :
statement(s);
break; /* optional */

case constant-expression :
statement(s);
break; /* optional /

/
you can have any number of case statements /
default : /
Optional */
statement(s);

}

6.Loops
There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages

while loop
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

for loop
Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.

do...while loop
Like a ‘while’ statement, except that it tests the condition at the end of the loop body.

Foreach loop
Foreach loop is used to access elements of an array quickly without performing initialization, testing and increment/decrement. The working of foreach loops is to do something for every element rather than doing something n times.

7.Break
The break statement can also be used to jump out of a loop.

8.Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

9.Pointers
A pointer however, is a variable that stores the memory address as its value.
A pointer variable points to a data type (like int or string) of the same type, and is created with the * operator.

There are three ways to declare pointer variables, but the first way is preferred:
int a; // Preferred
int
a;
int * a;

Dereferencing In the example from the previous page, we used the pointer variable to get the memory address of a variable (used together with the & reference operator). However, you can also use the pointer to get the value of the variable, by using the * operator (the dereference operator):

Note that the * sign can be confusing here, as it does two different things in our code:
When used in declaration (int *a), it creates a pointer variable.
When not used in declaration, it act as a dereference operator.

Modify the Pointer Value
We can also change the pointer's value. But note that this will also change the value of the original variable:
Refer the examples Pointer1 and Pointer for better understanding

Some practice example for implementation
1.Discountcalculation.cpp
2.leapyear.cpp
3.factorial.cpp

Pointer to function

In C++, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example(Pointertofunction1.cpp & Pointertofunction2.cpp ) that shows declaration and function call using function pointer.

10.Array
An array is a collection of elements of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier.
In C++, the index of the first array element is always zero. As expected, an n array must be declared prior its use. A typical declaration for an array in C++ is:
type name [elements];

Different ways to INITIALIZING ARRAYS
int a [5] = { };
int a [5] = { 16, 2, 77, 40, 12071 };
int a [5] = { 10, 20, 30 };
int a [] = { 16, 2, 77, 40, 12071 };

ARRAY ACCESSING
The values of any of the elements in an array can be accessed just like the value of a regular variable of the same type. The syntax is:
name[index]

int foo[5]; // declaration of a new array
foo[2] = 75; // access to an element of the array.

Multidimensional arrays can be described as "arrays of arrays". For example, a bi-dimensional array can be imagined as a two-dimensional table made of elements, all of
them hold same type of elements.
The C++ syntax for this is

int Table [3][5];

11.Templates in C++
A template is a simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. For example, if i need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter.
C++ adds two new keywords to support templates: ‘template’ and ‘typename’ The second keyword can always be replaced by keyword ‘class’.
For esay understanding check templeteclass.cpp file

12.Terminology
Formal Parameter : A variable and its type as they appear in the prototype of the function or method.
Actual Parameter : The variable or expression corresponding to a formal parameter that appears in the function or method call in the calling environment.

For esay understanding check argumnet2.cpp file


13.Important methods of Parameter Passing
(i).Pass By Value : This method uses in-mode semantics. Changes made to formal parameter do not get transmitted back to the caller. Any modifications to the formal parameter variable inside the called function or method affect only the separate storage location and will not be reflected in the actual parameter in the calling environment

(ii).Pass by reference(aliasing)
This technique uses in/out-mode semantics. Changes made to formal parameter do get transmitted back to the caller through parameter passing. Any changes to the formal parameter are reflected in the actual parameter in the calling environment as formal parameter receives a reference (or pointer) to the actual data. This method is also called as call by reference. This method is efficient in both time and space.

(iii).Passing arguments by address
There is one more way to pass variables to functions, and that is by address. Passing an argument by address involves passing the address of the argument variable rather than the argument variable itself.

14.Class and Object
A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called member variables and member
functions) as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private. The
private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are
accessible outside the class.
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated


For better understanding Refer to class.cpp file

15.Data-Hiding
In simple words, data hiding is an object-oriented programming technique of hiding internal object details i.e. data members. Data hiding guarantees restricted data access
to class members & maintain object integrity.
For better understanding Refer to DataHiding1.cpp file
16.Getter and Setter
Getters and Setters allow you to effectively protect your data. This is a technique used greatly when creating classes. For each variable, a get method will return its value
and a set method will set the value. ... The getters and setters are usually public and the variables are made private.

For better understanding Refer to DataHiding2.cpp file

17.Constructor
A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is
special member function of the class.

How constructors are different from a normal member function?

A constructor is different from normal functions in following ways:
1.Constructor has same name as the class itself
2.Constructors don’t have return type
3.A constructor is automatically called when an object is created.
If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).

18Parameterized Constructors:
It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply
add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.

For better understanding refer to Parameterizedconstructor.cpp

19.Copy Constructor
A copy constructor is a special constructor for creating a new object as a copy of an existing object. Copy constructors are the standard way of copying objects in C++, as opposed to cloning, and have C++-specific nuances.

For better understanding refer to Parameterizedconstructor.cpp

20.Scope Resolution Operator
The :: (scope resolution) operator is used to get hidden names due to variable scopes so that you can still use them. The scope resolution operator can be used as both unary and binary. You can use the unary scope operator if a namespace scope or global scope name is hidden by a particular declaration of an equivalent name during a block or class.

For better understanding refer to scope_resolution.cpp
21.Inline function
C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.

To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.
For better understanding refer to InlineFunction.cpp

22.This Pointer
In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++.
1.It can be used to pass current object as a parameter to another method.
2.It can be used to refer current class instance variable.
3.It can be used to declare indexers.

For better understanding refer to ThisPointer.cpp

23.Strcture And Class
In C++, a structure is the same as a class except for a few differences. The most important of them is security. A Structure is not secure and cannot hide its implementation details from the end user while a class is secure and can hide its programming and designing details
Members of a class are private by default and members of a struct are public by default.
When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private.

For better understanding refer to StructureAndClass.cpp

24.Inheritance
In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object or class, retaining similar implementation. Also defined as deriving new classes from existing ones such as super class or base class and then forming them into a hierarchy of classes.

25.Access Specifiers
C++ access specifiers are used for determining or setting the boundary for the availability of class members (data members and member functions) beyond that class.
For example, the class members are grouped into sections, private protected and public. These keywords are called access specifiers which define the accessibility or visibility level of class members.

(i).Public Access Specifier
If public access specifier is used while deriving class then the public data members of the base class becomes the public member of the derived class and protected members becomes the protected in the derived class but the private members of the base class are inaccessible.

For better understanding refer to Public_Access_Specifier.cpp.cpp

(ii).Protected Access Specifier
If protected access specifier is used while deriving class then the public and protected data members of the base class becomes the protected member of the derived class and private member of the base class are inaccessible.
In this case, the members of the base class can be used only within the derived class as protected members except for the private members.

For better understanding refer to Protected_Access_Specifier.cpp.cpp

(iii).Private Access Specifier
If private access specifier is used while creating a class, then the public and protected data members of the base class become the private member of the derived class and private member of base class remains private.
In this case, the members of the base class can be used only within the derived class and cannot be accessed through the object of derived class whereas they can be accessed by creating a function in the derived class.

For better understanding refer to Private_Access_Specifier.cpp



26.Single Inheritance
If a single class is derived from one base class then it is called single inheritance.
In C++ single inheritance base and derived class exhibit one to one relation.

For better understanding refer to single_inheritance.cpp


27.Hierarchical Inheritance
When several classes are derived from common base class it is called hierarchical inheritance.
In C++ hierarchical inheritance, the feature of the base class is inherited onto more than one sub-class.
For example, a car is a common class from which Audi, Ferrari, Maruti etc can be derived.

For better understanding refer to Hierarchical_inheritance.cpp


28.Multilevel Inheritance
If a class is derived from another derived class then it is called multilevel inheritance.
So in C++ multilevel inheritance, a class has more than one parent class.

For example, if we take animals as a base class then mammals are the derived class which has features of animals and then humans are the also derived class that is derived from sub-class mammals which inherit all the features of mammals.

For better understanding refer to multilevel_inheritance.cpp


29.Mutiple Inheritance
To put it in simple words, in multilevel inheritance, a class is derived from a class which is also derived from another base class. And these levels of inheritance can be extended. On the contrary, in multiple inheritance, a class is derived from two different base classes.

For better understanding refer to Multiple_inheritance.cpp


30.Hybrid Inheritance The inheritance in which the derivation of a class involves more than one form of any inheritance is called hybrid inheritance. Basically C++ hybrid inheritance is combination of two or more types of inheritance. It can also be called multi path inheritance.

For better understanding refer to Hybrid_inheritance.cpp


31.Polymorphism
Polymorphism is a feature of OOPs that allows the object to behave differently in different conditions. In C++ we have two types of polymorphism:

  1. Compile time Polymorphism – This is also known as static (or early) binding.
    Function overloading and Operator overloading are perfect example of Compile time polymorphism.

  2. Runtime Polymorphism – This is also known as dynamic (or late) binding.
    Function overriding is an example of Runtime polymorphism.

32.Function Overloading
Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list.
For better understanding refer to FunctionOverloading.cpp

33.Function Overriding
Function overriding is a feature that allows us to have a same function in child class which is already present in the parent class. A child class inherits the data members and member functions of parent class, but when you want to override a functionality in the child class then you can use function overriding. It is like creating a new version of an old function, in the child class.

For better understanding refer to FunctionOverriding.cpp

34.Operators Overloading
Operators Overloading in C++... we can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with user-defined types as well.
Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

For better understanding refer to OperatorOverloading.cpp

35.Operator Overloading using a Friend function:
In this approach, the operator overloading function must precede with friend keyword, and declare a function class scope. ll the working and implementation would same as binary operator function except this function will be implemented outside of the class scope.

For better understanding refer to OperatorOverloadingUsingFriendFunction.cpp

36.Virtual Function
A virtual function is a member function which is declared within a base class and is re-defined(Overriden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.

For better understanding refer to VirtualFunction.cpp and Polymorphism.cpp

37.Abstract Class
An abstract class in C++ is a class that has at least one pure virtual function (i.e., a function that has no definition). The classes inheriting the abstract class must provide a definition for the pure virtual function; otherwise, the subclass would become an abstract class itself.

For better understanding refer to VirtualFunction.cpp and Abstract_Class.cpp

38.Friend Function
A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

For better understanding refer to Friend_function.cpp

39.Friend Class
A friend class in C++ can access the private and protected members of the class in which it is declared as a friend.A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main class representing that data structure. The friend class mechanism allows to extend the storage and access to the parts, while retaining proper encapsulation as seen by the users of the data structure.

For better understanding refer to Friend_class.cpp

40.Static Members
We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.
A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.

For better understanding refer to Static_member.cpp

41.Static Members Function
By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.
A static member function can only access static data member, other static member functions and any other functions from outside the class.

For better understanding refer to Static_member_function.cpp

42.Nested Class
A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing class and has the same access rights as the other members. However, the member functions of the enclosing class have no special access to the members of a nested class.

For better understanding refer to Nested_Class.cpp

43.Anonymous Class
A class with no name provided is known as an anonymous class in c++. An anonymous class is a special class with one basic property.
As there is no name given to the class there is no constructor allocated to it, though a destructor is there for deallocating the memory block.
The class cannot be used as an element of a function i.e. we cannot pass it as an argument or cannot accept values return from the function.

For better understanding refer to Anonymous_Class.cpp

44.Exception Handling
An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.

throw − A program throws an exception when a problem shows up. This is done using a throw keyword.

catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

try − A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

Assuming a block will raise an exception, a method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch

For better understanding refer to Exception Handling.cpp , Communicate_between_try_and_throw_with_function , All_About_Throw.cpp and All_about_Catch.cpp

45.Namespaces in c++
A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.

For better understanding refer to Namespace.cpp

46.Constructor and Destructor in C++
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.
A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.

For better understanding refer to Destructor.cpp

47.Order Of Execution
C++ constructor call order will be from top to down that is from base class to derived class .
But c++ destructor call order will be in reverse order.

For better understanding refer to Order_of_Execution.cpp

48.Virtual destructors
Destructors in the Base class can be Virtual. Whenever Upcasting is done, Destructors of the Base class must be made virtual for proper destrucstion of the object when the program exits.
Virtual destructors are useful when you might potentially delete an instance of a derived class through a pointer to base class.
NOTE: Constructors are never Virtual, only Destructors can be Virtual.

For better understanding refer to Virtual_Destructor.cpp

49.Input/output streams
C++ input/output streams are primarily defined by iostream, a header file that is part of the C++ standard library (the name stands for Input/Output Stream). In C++ and its predecessor, the C programming language, there is no special syntax for streaming data input or output. Instead, these are combined as a library of functions. Like the cstdio header inherited from C's stdio.h, iostream provides basic input and output services for C++ programs. iostream uses the objects cin, cout, cerr, and clog for sending data to and from the standard streams input, output, error (unbuffered), and log (buffered) respectively. As part of the C++ standard library, these objects are a part of the std namespace.

50.Stream class hierarchy in c++


51.Data type
(i).ofstream
This data type represents the output file stream and is used to create files and to write information to files.
(ii).ifstream
This data type represents the input file stream and is used to read information from files.
(iii).fstream
This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

52.Opening a File
A file must be opened before you can read from it or write to it. Either ofstream or fstream object may be used to open a file for writing. And ifstream object is used to open a file for reading purpose only.

Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.

void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.

53.Mode Flag & Description
i) ios::app
Append mode. All output to that file to be appended to the end.
ii) ios::ate
Open a file for output and move the read/write control to the end of the file.
iii) ios::in
Open a file for reading.

iv) ios::out
Open a file for writing.

v) ios::trunc
If the file already exists, its contents will be truncated before opening the file.

For better understanding refer to File_Handling.cpp ,File_Handling1.cpp and File_Handling2.cpp