Professional Documents
Culture Documents
C++ UNIT-I
C++ UNIT-I
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.
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.
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
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.
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.
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
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++ 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.
#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
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;
}