0% found this document useful (0 votes)
40 views24 pages

NOTES OOPs & C++

C++ is an object-oriented programming language developed by Bjarne Stroustrup in the 1980s as an extension of C, incorporating features like classes and objects. It supports multiple programming paradigms and offers features such as memory management, a rich library, and faster compilation. The document also covers basic concepts of C++, including data types, operators, control structures, and input/output operations.

Uploaded by

Chitransh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views24 pages

NOTES OOPs & C++

C++ is an object-oriented programming language developed by Bjarne Stroustrup in the 1980s as an extension of C, incorporating features like classes and objects. It supports multiple programming paradigms and offers features such as memory management, a rich library, and faster compilation. The document also covers basic concepts of C++, including data types, operators, control structures, and input/output operations.

Uploaded by

Chitransh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

BY :- MR.

ANKIT KUMAR BALIYAN


9720488880/9997640710
BCA DEPARTMENT

DEPARTMENT OF COMPUTER APPLICATION


MCA-202
OOPs & C++

(Introduction Basic terms and ideas)

INTRODUCTION

C++ is an object-oriented programming language. It is an extension to C programming.

C++ is a general purpose, case-sensitive, free-form programming language that supports


object-oriented, procedural and generic programming.

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.

Bjarne Stroustrup is known as the founder of C++ language.

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.

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.
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.

Object-Oriented Programming (OOPs)


C++ supports the object-oriented programming, the four major pillar of object-oriented
programming (OOPs) used in C++ are:

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.

In C++, we use Function overloading and Function overriding to achieve polymorphism.

Abstraction

Hiding internal details and showing functionality is known as abstraction. For example: phone call,
we don't know the internal processing.

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


BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
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.

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.

Rules for defining variables


A variable can have alphabets, digits and underscore.
A variable name can start with alphabet and underscore only. It can't start with digit.
No white space is allowed within variable name.
A variable name must not be any reserved word or keyword e.g. char, float etc.
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT

Data Types
A data type specifies the type of data that a variable can store such as integer, floating, character etc.

There are 4 types of data types in C++ language.

Types Data Types

Basic Data Type int, char, float, double, etc

Derived Data Type array, pointer, etc

Enumeration Data Type enum

User Defined Data Type structure

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. for(initialization; condition; incr/decr){


2. //code to be executed
3. }

1. #include <iostream>
2. using namespace std;
3. int main() {
4. for(int i=1;i<=10;i++){
5. cout<<i <<"\n";
6. }
7. }

Nested For Loop


In C++, we can use for loop inside another for loop, it is known as nested for loop. The inner loop is
executed fully when outer loop is executed one time. So if outer loop and inner loop are executed 4
times, inner loop will be executed 4 times for each outer loop i.e. total 16 times.

C++ Nested For Loop Example


Let's see a simple example of nested for loop in C++.
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
1. #include <iostream>
2. using namespace std;
3.
4. int main () {
5. for(int i=1;i<=3;i++){
6. for(int j=1;j<=3;j++){
7. cout<<i<<" "<<j<<"\n";
8. }
9. }
10. }

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.

I/O Library Header Files


Let us see the common header files used in C++ programming are:

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):

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:

Value of ary is: Welcome to C++

Standard input stream (cin)


BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
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):

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:

Enter your age: 22


Your age is: 22

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):

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.

Syntax of new operator

1. type variable = new type(parameter_list);

Let's understand the new operator through an example.

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;

Some important points related to delete operator are:

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.

(Classes and Objects)


BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT

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. Smalltalk is considered as the first
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
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.

In C++, we use Function overloading and Function overriding to achieve


polymorphism.

Abstraction
Hiding internal details and showing functionality is known as abstraction. 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.

Object and Class


Since C++ is an object-oriented language, program is designed using objects and classes in C++.

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 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.

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. }

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.
BY :- MR. ANKIT KUMAR BALIYAN
9720488880/9997640710
BCA DEPARTMENT
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. }
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

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;
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:

201 Sonoo 990000


202 Nakul 29000

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.

There can be two types of constructors in C++.

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.

Let's see the simple example of C++ default Constructor.

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:

Default Constructor Invoked


Default Constructor Invoked

Parameterized Constructor
A constructor which has parameters is called parameterized constructor. It is used to provide different
values to distinct objects.

Let's see the simple example of C++ Parameterized Constructor.

#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:

101 Sonoo 890000


102 Nakul 59000

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.

C++ Constructor and Destructor Example


Let's see an example of constructor and destructor in C++ which is called automatically.

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

You might also like