NOTES OOPs & C++
NOTES OOPs & C++
INTRODUCTION
C++ is a middle-level language, as it encapsulates both high and low level language
features.
history
History of C++ language is interesting to know. Here we are going to discuss brief history of C++
language.
C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of
AT&T (American Telephone & Telegraph), located in U.S.A.
C++ programming is "relative" (called a superset) of C, it means any valid C program is also a valid
C++ program.
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
It was develop for adding a feature of OOP (Object Oriented Programming) in C without
significantly changing the C component
Features
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
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.
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
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.
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.
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
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.
1. Inheritance
2. Polymorphism
3. Encapsulation
4. Abstraction
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.
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.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example: phone call,
we don't know the internal processing.
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For
example: capsule, it is wrapped with different medicines.
Standard Libraries
Standard C++ programming is divided into three important parts:
5. The core library includes the data types, variables and literals, etc.
6. The standard library includes the set of functions manipulating strings, files, etc.
7. The Standard Template Library (STL) includes the set of methods manipulating a data
structure.
Review of C
Variable
A variable is a name of memory location. It is used to store data. Its value can be
changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily
identified.
Let's see the syntax to declare a variable:
1. type variable_list;
The example of declaring variable is given below:
1. int x;
2. float y;
3. char z;
Here, x, y, z are variables and int, float, char are data types.
Data Types
A data type specifies the type of data that a variable can store such as integer, floating, character etc.
Operators
An operator is simply a symbol that is used to perform operations. There can be
many types of operations like arithmetic, logical, bitwise etc.
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
There are following types of operators to perform different types of operations in C
language.
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
if-else
In C++ programming, if statement is used to test the condition. There are various types of if
statements in C++.
o if statement
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
o if-else statement
o nested if statement
o if-else-if ladder
IF Statement
The C++ if statement tests the condition. It is executed if condition is true.
1. if(condition){
2. //code to be executed
3. }
1. #include <iostream>
2. int main () {
3. int num = 10;
4. if (num % 2 == 0)
5. {
6. cout<<"It is even number";
7. }
8. return 0;
9. }
Output:/p>
It is even number
IF-else Statement
The C++ if-else statement also tests the condition. It executes if block if condition is true otherwise
else block is executed.
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
1. #include <iostream>
2. int main () {
3. int num = 11;
4. if (num % 2 == 0)
5. {
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
6. cout<<"It is even number";
7. }
8. else
9. {
10. cout<<"It is odd number";
11. }
12. return 0;
13. }
Output:
It is odd number
For Loop
The C++ for loop is used to iterate a part of the program several times. If the number of iteration is
fixed, it is recommended to use for loop than while or do-while loops.
The C++ for loop is same as C/C#. We can initialize variable, check condition and
increment/decrement value.
1. #include <iostream>
2. using namespace std;
3. int main() {
4. for(int i=1;i<=10;i++){
5. cout<<i <<"\n";
6. }
7. }
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
While loop
In C++, while loop is used to iterate a part of the program several times. If the number of iteration is
not fixed, it is recommended to use while loop than for loop.
1. while(condition){
2. //code to be executed
3. }
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int i=1;
5. while(i<=10)
6. {
7. cout<<i <<"\n";
8. i++;
9. }
10. }
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
Do-While Loop
The C++ do-while loop is used to iterate a part of the program several times. If the number of
iteration is not fixed and you must have to execute the loop at least once, it is recommended to use
do-while loop.
The C++ do-while loop is executed at least once because condition is checked after loop body.
1. do{
2. //code to be executed
3. }while(condition);
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int i = 1;
5. do{
6. cout<<i<<"\n";
7. i++;
8. } while (i <= 10) ;
9. }
C vs. C++
What is C?
C is a structural or procedural oriented programming language which is machine-independent and
extensively used in various applications.
C is the basic programming language that can be used to develop from the operating systems (like
Windows) to complex programs like Oracle database, Git, Python interpreter, and many more. C
programming language can be called a god's programming language as it forms the base for other
programming languages. If we know the C language, then we can easily learn other programming
languages. C language was developed by the great computer scientist Dennis Ritchie at the Bell
Laboratories. It contains some additional features that make it unique from other programming
languages.
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
What is C++?
C++ is a special-purpose programming language developed by Bjarne Stroustrup at Bell Labs circa
1980. C++ language is very similar to C language, and it is so compatible with C that it can run 99% of
C programs without changing any source of code though C++ is an object-oriented programming
language, so it is safer and well-structured programming language than C.
Basic Input/Output
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.
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. char ary[] = "Welcome to C++ ";
5. cout << "Value of ary is: " << ary << endl;
6. }
Output:
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. int age;
5. cout << "Enter your age: ";
6. cin >> age;
7. cout << "Your age is: " << age << endl;
8. }
Output:
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. cout << "C++ ";
5. cout << "End of line"<<endl;
6. }
Output:
C++
End of line
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
malloc() vs new in C++
Both the malloc() and new in C++ are used for the same purpose. They are used for allocating
memory at the runtime. But, malloc() and new have different syntax. The main difference between the
malloc() and new is that the new is an operator while malloc() is a standard library function that is
predefined in a stdlib header file.
What is new?
The new is a memory allocation operator, which is used to allocate the memory at the runtime. The
memory initialized by the new operator is allocated in a heap. It returns the starting address of the
memory, which gets assigned to the variable. The functionality of the new operator in C++ is similar
to the malloc() function, which was used in the C programming language. C++ is compatible with the
malloc() function also, but the new operator is mostly used because of its advantages.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int *ptr; // integer pointer variable declaration
6. ptr=new int; // allocating memory to the pointer variable ptr.
7. std::cout << "Enter the number : " << std::endl;
8. std::cin >>*ptr;
9. std::cout << "Entered number is " <<*ptr<< std::endl;
10. return 0;
11. }
Output:
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
Delete operator
It is an operator used in C++ programming language, and it is used to de-allocate
the memory dynamically. This operator is mainly used either for those pointers which
are allocated using a new operator or NULL pointer.
Syntax
1. delete pointer_name
For example, if we allocate the memory to the pointer using the new operator, and
now we want to delete it. To delete the pointer, we use the following statement:
1. delete p;
o It is either used to delete the array or non-array objects which are allocated by
using the new keyword.
o To delete the array or non-array object, we use delete[] and delete operator,
respectively.
o The new keyword allocated the memory in a heap; therefore, we can say that
the delete operator always de-allocates the memory from the heap
o It does not destroy the pointer, but the value or the memory block, which is
pointed by the pointer is destroyed.
OOPs Concepts
The major purpose of C++ programming is to introduce the concept of object
orientation to the C programming language.
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
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.
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.
Abstraction
Hiding internal details and showing functionality is known as abstraction. 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.
C++ Object
In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
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.
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. }
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. }
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
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;
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
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:
Constructor
In C++, constructor is a special method which is invoked automatically at the time of object creation.
It is used to initialize the data members of new object generally. The constructor in C++ has the same
name as class or structure.
o Default constructor
o Parameterized constructor
Default Constructor
A constructor which has no argument is known as default constructor. It is invoked at the time of
creating object.
1. #include <iostream>
2. using namespace std;
3. class Employee
4. {
5. public:
6. Employee()
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
7. {
8. cout<<"Default Constructor Invoked"<<endl;
9. }
10. };
11. int main(void)
12. {
13. Employee e1; //creating an object of Employee
14. Employee e2;
15. return 0;
16. }
Output:
Parameterized Constructor
A constructor which has parameters is called parameterized constructor. It is used to provide different
values to distinct objects.
#include <iostream>
using namespace std;
class Employee {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of
Employee
Employee e2=Employee(102, "Nakul", 59000);
e1.display();
e2.display();
return 0;
}
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
Output:
Destructor
A destructor works opposite to constructor; it destructs the objects of classes. It can be defined only
once in a class. Like constructors, it is invoked automatically.
A destructor is defined like constructor. It must have same name as class. But it is prefixed with a tilde
sign (~).
Note: C++ destructor cannot have parameters. Moreover, modifiers can't be applied on destructors.
1. #include <iostream>
2. using namespace std;
3. class Employee
4. {
5. public:
6. Employee()
7. {
8. cout<<"Constructor Invoked"<<endl;
9. }
10. ~Employee()
11. {
12. cout<<"Destructor Invoked"<<endl;
13. }
14. };
15. int main(void)
16. {
17. Employee e1; //creating an object of Employee
18. Employee e2; //creating an object of Employee
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
19. return 0;
20. }
Output:
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked