c++ basic
c++ basic
1. Simple
2. Abstract Data types
3. Machine Independent or Portable
4. Mid-level programming language
5. Structured programming language
6. Rich Library
7. Memory Management
8. Quicker Compilation
9. Pointers
10. Recursion
11. Extensible
12. Object-Oriented
13. Compiler based
14. Reusability
15. National Standards
16. Errors are easily detected
17. Power and Flexibility
18. Strongly typed language
19. Redefine Existing Operators
20. Modeling Real-World Problems
21. Clarity
1) Simple
C++ is a simple language because it provides a structured approach (to break the problem
into parts), a rich set of library functions, data types, etc.
3) Portable
C++ is a portable language and programs made in it can be run on different machines.
6) Rich Library
C++ provides a lot of inbuilt functions that make the development fast. Following are the
libraries used in C++ programming are:
o <iostream>
o <cmath>
o <cstdlib>
o <fstream>
7) Memory Management
C++ provides very efficient management techniques. The various memory management
operators help save the memory and improve the program's efficiency. These operators
allocate and deallocate memory at run time. Some common memory management
operators available C++ are new, delete etc.
8) Quicker Compilation
C++ programs tend to be compact and run quickly. Hence the compilation and execution
time of the C++ language is fast.
9) Pointer
C++ provides the feature of pointers. We can use pointers for memory, structures,
functions, array, etc. We can directly interact with the memory by using the pointers.
10) Recursion
In C++, we can call the function within the function. It provides code reusability for every
function.
11) Extensible
C++ programs can easily be extended as it is very easy to add new features into the
existing program.
12) Object-Oriented
In C++, object-oriented concepts like data hiding, encapsulation, and data abstraction
can easily be implemented using keyword class, private, public, and protected access
specifiers. Object-oriented makes development and maintenance easier.
14) Reusability
With the use of inheritance of functions programs written in C++ can be reused in any
other program of C++. You can save program parts into library files and invoke them in
your next programming projects simply by including the library files. New programs can
be developed in lesser time as the existing code can be reused. It is also possible to define
several functions with same name that perform different task. For Example: abs () is used
to calculate the absolute value of integer, float and long integer.
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
Object
Any entity that has state and behavior is known as an object. For example: chair, pen,
table, keyboard, bike etc. It can be physical and logical.
Class
Collection of objects is called class. It is a logical entity.
Inheritance
When one object acquires all the properties and behaviours of parent object i.e.
known as inheritance. It provides code reusability. It is used to achieve runtime
polymorphism.
1. Sub class - Subclass or Derived Class refers to a class that receives properties from another
class.
2. Super class - The term "Base Class" or "Super Class" refers to the class from which a
subclass inherits its properties.
3. Reusability - As a result, when we wish to create a new class, but an existing class already
contains some of the code we need, we can generate our new class from the old class
thanks to inheritance. This allows us to utilize the fields and methods of the pre-existing
class.
Polymorphism
When one task is performed by different ways i.e. known as polymorphism. For
example: to convince the customer differently, to draw something e.g. shape or rectangle
etc.
Different situations may cause an operation to behave differently. The type of data utilized
in the operation determines the behavior.
Abstraction
Hiding internal details and showing functionality is known as abstraction. Data
abstraction is the process of exposing to the outside world only the information that is
absolutely necessary while concealing implementation or background information.For
example: phone call, we don't know the internal processing.
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as
encapsulation. For example: capsule, it is wrapped with different medicines.
Dynamic Binding - In dynamic binding, a decision is made at runtime regarding the code
that will be run in response to a function call. For this, C++ supports virtual functions.
With the use of classes and objects, object-oriented programming makes code
maintenance simple. Because inheritance allows for code reuse, the program is simpler
because you don't have to write the same code repeatedly. Data hiding is also provided
by ideas like encapsulation and abstraction.
1) The main function must always be outside the class in C++ and is required. This means
that we may do without classes and objects and have a single main function in the
application.
It is expressed as an object in this case, which is the first time Pure OOP has been violated.
2) Global variables are a feature of the C++ programming language that can be accessed
by any other object within the program and are defined outside of it. Encapsulation is
broken here. Even though C++ encourages encapsulation for classes and objects, it
ignores it for global variables.
Overloading
Polymorphism also has a subset known as overloading. An existing operator or function
is said to be overloaded when it is forced to operate on a new data type.
C++ Object
In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.
In other words, object is an entity that has state and behavior. Here, state means data and
behavior means functionality.
Object is an instance of a class. All the members of the class can be accessed through
object.
Let's see an example to create object of student class using s1 as the reference variable.
In this example, Student is the type and s1 is the reference variable that refers to the
instance of Student class.
C++ Class
In C++, class is a group of similar objects. It is a template from which objects are created.
It can have fields, methods, constructors etc.
Let's see an example of C++ class that has three fields only.
1. class Student
2. {
3. public:
4. int id; //field or data member
5. float salary; //field or data member
6. String name;//field or data member
7. }
C++ Object and Class Example
Let's see an example of class that has two fields: id and name. It creates instance of the
class, initializes the object and prints the object value.
1. #include <iostream>
2. using namespace std;
3. class Student {
4. public:
5. int id;//data member (also instance variable)
6. string name;//data member(also instance variable)
7. };
8. int main() {
9. Student s1; //creating an object of Student
10. s1.id = 201;
11. s1.name = "Sonoo Jaiswal";
12. cout<<s1.id<<endl;
13. cout<<s1.name<<endl;
14. return 0;
15. }
Output:
201
Sonoo Jaiswal
1. #include <iostream>
2. using namespace std;
3. class Student {
4. public:
5. int id;//data member (also instance variable)
6. string name;//data member(also instance variable)
7. void insert(int i, string n)
8. {
9. id = i;
10. name = n;
11. }
12. void display()
13. {
14. cout<<id<<" "<<name<<endl;
15. }
16. };
17. int main(void) {
18. Student s1; //creating an object of Student
19. Student s2; //creating an object of Student
20. s1.insert(201, "Sonoo");
21. s2.insert(202, "Nakul");
22. s1.display();
23. s2.display();
24. return 0;
25. }
Output:
201 Sonoo
202 Nakul
1. #include <iostream>
2. using namespace std;
3. class Employee {
4. public:
5. int id;//data member (also instance variable)
6. string name;//data member(also instance variable)
7. float salary;
8. void insert(int i, string n, float s)
9. {
10. id = i;
11. name = n;
12. salary = s;
13. }
14. void display()
15. {
16. cout<<id<<" "<<name<<" "<<salary<<endl;
17. }
18. };
19. int main(void) {
20. Employee e1; //creating an object of Employee
21. Employee e2; //creating an object of Employee
22. e1.insert(201, "Sonoo",990000);
23. e2.insert(202, "Nakul", 29000);
24. e1.display();
25. e2.display();
26. return 0;
27. }
Output:
// access modifier
#include<iostream>
// class definition
class Circle
{
public:
double radius;
double compute_area()
return 3.14*radius*radius;
};
// main function
int main()
Circle obj;
obj.radius = 5.5;
Output:
Radius is: 5.5
Area is: 94.985
In the above program, the data member radius is declared as public so it could
be accessed outside the class and thus was allowed access from inside
main().
2. Private: The class members declared as private can be accessed only by
the member functions inside the class. They are not allowed to be accessed
directly by any object or function outside the class. Only the member functions
or the friend functions are allowed to access the private data members of the
class.
// access modifier
#include<iostream>
class Circle
private:
double radius;
public:
double compute_area()
return 3.14*radius*radius;
};
// main function
int main()
Circle obj;
return 0;
Output:
In function 'int main()':
11:16: error: 'double Circle::radius' is private
double radius;
^
31:9: error: within this context
obj.radius = 1.5;
^
The output of the above program is a compile time error because we are not
allowed to access the private data members of a class directly from outside the
class. Yet an access to obj.radius is attempted, but radius being a private data
member, we obtained the above compilation error.
However, we can access the private data members of a class indirectly using
the public member functions of the class.
Example:
CPP
// access modifier
#include<iostream>
class Circle
private:
double radius;
public:
void compute_area(double r)
radius = r;
};
// main function
int main()
Circle obj;
obj.compute_area(1.5);
return 0;
Output:
Radius is: 1.5
Area is: 7.065
3. Protected: The protected access modifier is similar to the private access
modifier in the sense that it can’t be accessed outside of its class unless with
the help of a friend class. The difference is that the class members declared as
Protected can be accessed by any subclass (derived class) of that class as
well.
Note: This access through inheritance can alter the access modifier of the
elements of base class in derived class depending on the mode of Inheritance.
Example:
CPP
#include <bits/stdc++.h>
// base class
class Parent
protected:
int id_protected;
};
public:
id_protected = id;
void displayId()
};
// main function
int main() {
Child obj1;
obj1.setId(81);
obj1.displayId();
return 0;
Output:
id_protected is: 81