Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

riteshkandarics@gmail.

com iamriteshkandari 9259069231

UNIT I:

What is C++
• 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.

Why Use C++


• C++ is one of the world's most popular programming languages.
• C++ is an object-oriented programming language which gives a clear structure to programs
and allows code to be reused, lowering development costs.
• C++ can be found in today's operating systems, Graphical User Interfaces, and embedded
systems.
• C++ is portable and can be used to develop applications that can be adapted to multiple
platforms.
• C++ is fun and easy to learn!
• As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice
versa

What is OOP (Object-Oriented Programming):


Procedural-oriented programming is about writing procedures or functions that perform
operations on the data, while object-oriented programming is about creating objects that contain
both data and functions.
Object-oriented programming: OOPs refers to languages that uses objects in programming.
Object-oriented programming aims to implement real-world entities like inheritance, hiding,
polymorphism etc. in programming. The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of the code can access this data except that
function.
Object-oriented programming has several advantages over procedural programming:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP makes it possible to create full reusable applications with less code and shorter
development time
riteshkandarics@gmail.com iamriteshkandari 9259069231

OOPs Concepts:
1) Class
2) Object
3) Data Abstraction
4) Data Encapsulation
5) Inheritance
6) Polymorphism

Class: A class is a blu-print representing a group of objects that share common properties and
relationships. For example: We can take Human Being as a class. Human Being, having body
parts, and performing various actions.

Object: object is an identifiable entity with some characteristics and behaviour. For example,
we can say orange is an object. Its characteristics are: it is spherical shaped, its colour is orange
etc. Its behaviour is: it is juicy and its tastes sweet-sour.
In real world many examples of object and class like dog, cat, and cow are belong to animal's
class. Each object has state and behaviors. For example, a dog has state: - color, name, height,
age as well as behaviors: - barking, eating, and sleeping.

Vehicle class
Car, bike, truck these all are belongs to vehicle class. These Objects have also different states
and behaviors. For Example, car has state - color, name, model, speed, Mileage. as we; as
behaviors - distance travel
riteshkandarics@gmail.com iamriteshkandari 9259069231

Data Abstraction: Abstraction refers to the act of representing essential features without
including the background details or explanations.
For example: you are driving a car. You only know the essential features to drive a car e.g.,
gear handling, steering handling, use of clutch, accelerator, breaks etc. but while driving do
you get into internal details of car like wiring, motor working etc.? you just change the gears
or apply the breaks etc. what is happening inside the hidden from you. This is abstraction where
you only know the essential things to drive a car without including the background details or
explanations.

Data Encapsulation: The wrapping up of data and operations / functions (that operate on the
data) into a single unit (called class) is known as Encapsulation.
Combining of state and behavior in a single container is known as encapsulation. In C++
language encapsulation can be achieve using class keyword, state represents declaration of
variables on attributes and behavior represents operations in terms of method.

Inheritance: Inheritance is the capability of one class of things to inherit capabilities or


properties from another class. For Example: The real-life example of inheritance is child and
parents; all the properties of father are inherited by his son.
riteshkandarics@gmail.com iamriteshkandari 9259069231

Advantage of inheritance
If we develop any application using this concept than that application have following
advantages,
• Application development time is less.
• Application takes less memory.
• Application execution time is less.
• Application performance is enhanced (improved).
• Redundancy (repetition) of the code is reduced or minimized so that we get
consistence results and less storage cost.

Types of Inheritance
Based on number of ways inheriting the feature of base class into derived class it has five types
they are:
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multiple inheritance
• Hybrid inheritance

Polymorphism: Polymorphism is the ability for a message or data to be processed in more


than one form.
Suppose if you are in class room that time you behave like a student, when you are in market
at that time you behave like a customer, when you at your home at that time you behave like a
son or daughter, here one person has different-different behaviors.
riteshkandarics@gmail.com iamriteshkandari 9259069231

There are two types of polymorphism.


1) Compile time polymorphism - It is achieved by overloading functions and operators
2) Run time polymorphism - It is achieved by overriding virtual functions

Benefits of OOP
• OOP models complex things as reproducible, simple structures
• Reusable, OOP objects can be used across programs
• Allows for class-specific behavior through polymorphism
• Easier to debug, classes often contain all applicable information to them
• Secure, protects information through encapsulation

C++ Features
C++ is object-oriented programming language. It provides a lot of features that are given
below.
• Simple
• Machine Independent or Portable
• Mid-level programming language
• Structured programming language
• Rich Library
• Memory Management
• Fast Speed
• Pointers
• Recursion
• Extensible
• Object Oriented
• Compiler based

1) Simple
C++ is a simple language in the sense that it provides structured approach (to break the problem
into parts), rich set of library functions, data types etc.

2) Machine Independent or Portable


Unlike assembly language, c programs can be executed in many machines with little bit or no
change. But it is not platform-independent.
riteshkandarics@gmail.com iamriteshkandari 9259069231

3) Mid-level programming language


C++ is also used to do low level programming. It is used to develop system applications such as
kernel, driver etc. It also supports the feature of high-level language. That is why it is known as
mid-level language.

4) Structured programming language


C++ is a structured programming language in the sense that we can break the program into parts
using functions. So, it is easy to understand and modify.

5) Rich Library
C++ provides a lot of inbuilt functions that makes the development fast.

6) Memory Management
It supports the feature of dynamic memory allocation. In C++ language, we can free the allocated
memory at any time by calling the free() function.

7) Speed
The compilation and execution time of C++ language is fast.

8) Pointer
C++ provides the feature of pointers. We can directly interact with the memory by using the
pointers. We can use pointers for memory, structures, functions, array etc.

9) Recursion
In C++, we can call the function within the function. It provides code reusability for every
function.

10) Extensible
C++ language is extensible because it can easily adopt new features.

11) Object Oriented


C++ is object-oriented programming language. 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.

12) Compiler based


C++ is a compiler-based programming language, it means without compilation no C++ program
can be executed. First, we need to compile our program using compiler and then we can execute
our program.
riteshkandarics@gmail.com iamriteshkandari 9259069231

Difference between POP vs OOP

No. C C++
1) C follows the procedural style C++ is multi-paradigm. It supports
programming. both procedural and object oriented.
2) Data is less secured in C. In C++, you can use modifiers for class
members to make it inaccessible for outside
users.
3) C follows the top-down approach. C++ follows the bottom-up approach.
4) C does not support function C++ supports function overloading.
overloading.
5) In C, you can't use functions in In C++, you can use functions in structure.
structure.
6) C does not support reference variables. C++ supports reference variables.
7) In C, scanf() and printf() are mainly C++ mainly uses stream cin and cout to
used for input/output. perform input and output operations.
8) Operator overloading is not possible in Operator overloading is possible in C++.
C.
9) C programs are divided C++ programs are divided into functions
into procedures and modules and classes.
10) C does not provide the feature of C++ supports the feature of namespace.
namespace.
11) Exception handling is not easy in C. It C++ provides exception handling using Try
has to perform using other functions. and Catch block.
12) C does not support the inheritance. C++ supports inheritance

The main function


main() function is the entry point of any C++ program. It is the point at which execution of
program is started. When a C++ program is executed, the execution control goes directly to the
main() function. Every C++ program have a main() function.
Syntax
void main()
{
............
}
riteshkandarics@gmail.com iamriteshkandari 9259069231

In above syntax;
• void: is a keyword in C++ language, void means nothing, whenever we use void as a
function return type then that function nothing return. here main() function no return any
value.
• In place of void we can also use int return type of main() function, at that time main()
return integer type value.
• main: is a name of function which is predefined function in C++ library.
Simple example of main()
Example
#include<stdio.h>
void main()
{
cout<<"This is main function";
}

C++ preprocessors
Preprocessor directives are one of the unique features of C++. It provides many tools that other
high-level language does not, and programmers can use these tools to create efficient, easy-to-
read, easy-to-modify and portable C ++ programs.
Before a C++ program gets compiled by the compiler, the source-code gets processed by the
compiler. This technique is called preprocessor and the process is called preprocessing. It is
separate program part that the C++ compiler invokes it at the first part of the translation. This
technique is not a part of the compiler but it is a separate method that comes under compilation
process. It directs the compiler that the information should be preprocessed before the actual
compilation starts.
All preprocessor directives in C++ begin with #, and they do not need to end with a
semicolon (;) because this is not a statement in C++.
The #define directive creates a symbolic constant and these symbolic constants are called macro.

<iostream.h> File
It is the predefined library function used for input and output also called as header files.
iostream is the header file which contains all the functions of program like cout, cin
etc. and #include tells the preprocessor to include these header file in the program.
include <iostream.h> #include is known as a preprocessor directive, which is used to load files.
<> indicate the start and end of file name to be loaded. you can use " " quotes too instead of <>.
<> are for header files (files with extension .h) "" for c files (files with extension .c)
riteshkandarics@gmail.com iamriteshkandari 9259069231

The file iostream.h is located in your include path. The include path indicates the directories on
your computer in which to search for a file, if the file is not located in the current directory.
iostream.h is a file containing definitions for input/output functions that you use in your
program.

C++ input and output with cin and cout


• C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of
data. It makes the performance fast.
• If bytes flow from main memory to device like printer, display screen, or a network
connection, etc., this is called as output operation.
• If bytes flow from device like printer, display screen, or a network connection, etc to main
memory, this is called as input operation.

Standard output stream (cout)


The cout is a predefined object of ostream class. It is connected with the standard output device,
which is usually a display screen. The cout is used in conjunction with stream insertion operator
(<<) to display the output on a console
Let's see the simple example of standard output stream (cout):
#include <iostream>
void main () {
char ary [] = "Welcome to C++ tutorial";
cout << "Value of ary is: " << ary << endl;
}

Standard input stream (cin)


The cin is a predefined object of istream class. It is connected with the standard input device,
which is usually a keyboard. The cin is used in conjunction with stream extraction operator (>>)
to read the input from a console.
Let's see the simple example of standard input stream (cin):
#include <iostream>
void main( ) {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age << endl;
}
riteshkandarics@gmail.com iamriteshkandari 9259069231

Standard end line (endl)


The endl is a predefined object of ostream class. It is used to insert a new line characters and
flushes the stream.
Let's see the simple example of standard end line (endl):
#include <iostream.h>
void main () {
cout << "C++ Tutorial";
cout << " Javatpoint"<<endl;
cout << "End of line"<<endl;
}

C++ Variables
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). For
example,
int age = 14;
Rules for naming a variable
• A variable name can only have alphabets, numbers, and the underscore _.
• A variable name cannot begin with a number.
• Variable names should not begin with an uppercase character.
• A variable name cannot be a keyword. For example, int is a keyword that is used to denote
integers.
• A variable name can start with an underscore. However, it's not considered a good
practice.

C++ Fundamental Data Types


The table below shows the fundamental data types, their meaning, and their sizes (in bytes):

Data Type Meaning Size (in Bytes)


int Integer 2 or 4
float Floating-point 4
double Double Floating-point 8
char Character 1
bool Boolean 1
void Empty 0
riteshkandarics@gmail.com iamriteshkandari 9259069231

C++ Type Conversion


C++ allows us to convert data of one type to that of another. This is known as type conversion.
There are two types of type conversion in C++.
• Implicit Conversion
• Explicit Conversion (also known as Type Casting)

Implicit Type Conversion


The type conversion that is done automatically done by the compiler is known as implicit type
conversion. This type of conversion is also known as automatic conversion.
// Working of implicit type-conversion
#include <iostream.h>
int main() {
// assigning an int value to num_int
int num_int = 9;
// declaring a double type variable
double num_double;
// implicit conversion
// assigning int value to a double variable
num_double = num_int;
cout << "num_int = " << num_int << endl;
cout << "num_double = " << num_double << endl;
return 0;
}
In the program, we have assigned an int data to a double variable.
num_double = num_int;
Here, the int value is automatically converted to double by the compiler before it is assigned to
the num_double variable. This is an example of implicit type conversion.

Explicit Type Conversion


When the user manually changes data from one type to another, this is known as explicit
conversion. This type of conversion is also known as type casting.
There are three major ways in which we can use explicit conversion in C++. They are:
1. C-style type casting (also known as cast notation)
2. Function notation (also known as old C++ style type casting)
3. Type conversion operators
riteshkandarics@gmail.com iamriteshkandari 9259069231

#include <iostream.h>
int main() {
// initializing a double variable
double num_double = 3.56;
cout << "num_double = " << num_double << endl;
// C-style conversion from double to int
int num_int1 = (int)num_double;
cout << "num_int1 = " << num_int1 << endl;
// function-style conversion from double to int
int num_int2 = int(num_double);
cout << "num_int2 = " << num_int2 << endl;
return 0;
}

C++ Operators
Operators are symbols that perform operations on variables and values. For example, + is an
operator used for addition, while - is an operator used for subtraction.
Operators in C++ can be classified into 6 types:
1) Arithmetic Operators
2) Assignment Operators
3) Relational Operators
4) Logical Operators
5) Bitwise Operators
6) Other Operators

C++ Memory Management: new and delete


C++ allows us to allocate the memory of a variable or an array in run time. This is known as
dynamic memory allocation.
In other programming languages such as Java and Python, the compiler automatically manages
the memories allocated to variables. But this is not the case in C++.
In C++, we need to deallocate the dynamically allocated memory manually after we have no use
for the variable.
We can allocate and then deallocate memory dynamically using the new and delete operators
respectively.
riteshkandarics@gmail.com iamriteshkandari 9259069231

C++ new Operator


The new operator allocates memory to a variable. For example,
// declare an int pointer
int* pointVar;
// dynamically allocate memory
// using the new keyword
pointVar = new int;
// assign value to allocated memory
*pointVar = 45;
Here, we have dynamically allocated memory for an int variable using the new operator.

delete Operator
Once we no longer need to use a variable that we have declared dynamically, we can deallocate
the memory occupied by the variable. For this, the delete operator is used. It returns the memory
to the operating system. This is known as memory deallocation.
The syntax for this operator is
delete pointerVariable;
Consider the code:
// declare an int pointer
int* pointVar;
// dynamically allocate memory
// for an int variable
pointVar = new int;
// assign value to the variable memory
*pointVar = 45;
// print the value stored in memory
cout << *pointVar; // Output: 45
// deallocate the memory
delete pointVar;
Here, we have dynamically allocated memory for an int variable using the pointer pointVar.
After printing the contents of pointVar, we deallocated the memory using delete.

Reference Variable
When a variable is declared as a reference, it becomes an alternative name for an existing
variable. A variable can be declared as a reference by putting ‘&’ in the declaration.
riteshkandarics@gmail.com iamriteshkandari 9259069231

#include<iostream.h>
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
// Value of x is now changed to 20
ref = 20;
cout << "x = " << x << endl ;
// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << endl ;
return 0;
}

You might also like