Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

c++ basic

Uploaded by

ketankumar2614
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

c++ basic

Uploaded by

ketankumar2614
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

C++ Features

C++ is a widely used programming language.

It provides a lot of features that are given below.

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.

2) Abstract Data types


In C++, complex data types called Abstract Data Types (ADT) can be created using classes.

3) Portable
C++ is a portable language and programs made in it can be run on different machines.

4) Mid-level / Intermediate programming language


C++ includes both low-level programming and high-level language so it is known as a
mid-level and intermediate programming language. It is used to develop system
applications such as kernel, driver, etc.

5) Structured programming language


C++ is a structured programming language. In this we can divide the program into several
parts using functions.

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.

13) Compiler based


C++ is a compiler-based programming language, which means no C++ program can be
executed without compilation. C++ compiler is easily available, and it requires very little
space for storage. First, we need to compile our program using a compiler, and then we
can execute our program.

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.

15) National Standards


C++ has national standards such as ANSI.

16) Errors are easily detected


It is easier to maintain a C++ programs as errors can be easily located and rectified. It also
provides a feature called exception handling to support error handling in your program.

17) Power and Flexibility


C++ is a powerful and flexible language because of most of the powerful flexible and
modern UNIX operating system is written in C++. Many compilers and interpreters for
other languages such as FORTRAN, PERL, Python, PASCAL, BASIC, LISP, etc., have been
written in C++. C++ programs have been used for solving physics and engineering
problems and even for animated special effects for movies.

18) Strongly typed language


The list of arguments of every function call is typed checked during compilation. If there
is a type mismatch between actual and formal arguments, implicit conversion is applied
if possible. A compile-time occurs if an implicit conversion is not possible or if the number
of arguments is incorrect.

19) Redefine Existing Operators


C++ allows the programmer to redefine the meaning of existing operators such as +, -
. For Example, The "+" operator can be used for adding two numbers and concatenating
two strings.

20) Modelling real-world problems


The programs written in C++ are well suited for real-world modeling problems as close
as possible to the user perspective.
21) Clarity
The keywords and library functions used in C++ resemble common English words.

C++ OOPs Concepts


The major purpose of C++ programming is to introduce the concept of object orientation
to the C programming language.

Object Oriented Programming is a paradigm that provides many concepts such


as inheritance, data binding, polymorphism etc.

The programming paradigm where everything is represented as an object is known as


truly object-oriented programming language.

OOPs (Object Oriented Programming System)


Object means a real word entity such as pen, chair, table etc. Object-Oriented
Programming is a methodology or paradigm to design a program using classes and
objects. It simplifies the software development and maintenance by providing some
concepts:

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.

A Class in C++ is the foundational element that leads to Object-Oriented programming.


A class instance must be created in order to access and use the user-defined data type's
data members and member functions. An object's class acts as its blueprint. Take the class
of cars as an example. Even if different names and brands may be used for different cars,
all of them will have some characteristics in common, such as four wheels, a speed limit,
a range of miles, etc. In this case, the class of car is represented by the wheels, the speed
limitations, and the mileage.

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.

In C++, we use abstract class and interface to achieve abstraction.

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.

Encapsulation is typically understood as the grouping of related pieces of information and


data into a single entity. Encapsulation is the process of tying together data and the
functions that work with it in object-oriented programming. Take a look at a practical
illustration of encapsulation: at a company, there are various divisions, including the sales
division, the finance division, and the accounts division. All financial transactions are
handled by the finance sector, which also maintains records of all financial data. In a
similar vein, the sales section is in charge of all tasks relating to sales and maintains a
record of each sale. Now, a scenario could occur when, for some reason, a financial official
requires all the information on sales for a specific month. Under the umbrella term "sales
section," all of the employees who can influence the sales section's data are grouped
together. Data abstraction or concealing is another side effect of encapsulation. In the
same way that encapsulation hides the data. In the aforementioned example, any other
area cannot access any of the data from any of the sections, such as sales, finance, or
accounts.

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.

Advantage of OOPs over Procedure-oriented


programming language
1. OOPs makes development and maintenance easier where as in Procedure-oriented
programming language it is not easy to manage if code grows as project size grows.
2. OOPs provide data hiding whereas in Procedure-oriented programming language a global
data can be accessed from anywhere.
3. OOPs provide ability to simulate real-world event much more effectively. We can provide
the solution of real word problem if we are using the Object-Oriented Programming
language.

Why do we need oops in C++?


There were various drawbacks to the early methods of programming, as well as poor
performance. The approach couldn't effectively address real-world issues since, similar to
procedural-oriented programming, you couldn't reuse the code within the program again,
there was a difficulty with global data access, and so on.

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.

Why is C++ a partial oop?


The object-oriented features of the C language were the primary motivation behind the
construction of the C++ language.

The C++ programming language is categorized as a partial object-oriented programming


language despite the fact that it supports OOP concepts, including classes, objects,
inheritance, encapsulation, abstraction, and polymorphism.

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 a runtime entity, it is created at runtime.

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.

1. Student s1; //creating an object of Student

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

C++ Class Example: Initialize and Display data


through method
Let's see another example of C++ class where we are initializing and displaying object
through method.

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

C++ Class Example: Store and Display Employee


Information
Let's see another example of C++ class where we are storing and displaying employee
information using method.

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:

201 Sonoo 990000


202 Nakul 29000
Access Modifiers in C++
Access modifiers are used to implement an important aspect of Object-Oriented
Programming known as Data Hiding.

There are 3 types of access modifiers available in C++:


1. Public
2. Private
3. Protected
Note: If we do not specify any access modifiers for the members inside the
class, then by default the access modifier for the members will be Private.
Let us now look at each one of these access modifiers in detail:
1. Public: All the class members declared under the public specifier will be
available to everyone. The data members and member functions declared as
public can be accessed by other classes and functions too. The public
members of a class can be accessed from anywhere in the program using the
direct member access operator (.) with the object of that class.
Example:
 CPP

// C++ program to demonstrate public

// access modifier

#include<iostream>

using namespace std;

// class definition

class Circle

{
public:

double radius;

double compute_area()

return 3.14*radius*radius;

};

// main function

int main()

Circle obj;

// accessing public datamember outside class

obj.radius = 5.5;

cout << "Radius is: " << obj.radius << "\n";

cout << "Area is: " << obj.compute_area();


return 0;

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.

C++ program to demonstrate private

// access modifier

#include<iostream>

using namespace std;

class Circle

// private data member

private:
double radius;

// public member function

public:

double compute_area()

{ // member function can access private

// data member radius

return 3.14*radius*radius;

};

// main function

int main()

// creating object of the class

Circle obj;

// trying to access private data member

// directly outside the class


obj.radius = 1.5;

cout << "Area is:" << obj.compute_area();

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

// C++ program to demonstrate private

// access modifier
#include<iostream>

using namespace std;

class Circle

// private data member

private:

double radius;

// public member function

public:

void compute_area(double r)

{ // member function can access private

// data member radius

radius = r;

double area = 3.14*radius*radius;

cout << "Radius is: " << radius << endl;

cout << "Area is: " << area;


}

};

// main function

int main()

// creating object of the class

Circle obj;

// trying to access private data member

// directly outside the class

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

// C++ program to demonstrate

// protected access modifier

#include <bits/stdc++.h>

using namespace std;

// base class

class Parent

// protected data members

protected:

int id_protected;

};

// sub class or derived class from public base class


class Child : public Parent

public:

void setId(int id)

// Child class is able to access the inherited

// protected data members of base class

id_protected = id;

void displayId()

cout << "id_protected is: " << id_protected << endl;

};

// main function
int main() {

Child obj1;

// member function of the derived class can

// access the protected data members of the base class

obj1.setId(81);

obj1.displayId();

return 0;

Output:
id_protected is: 81

You might also like