Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

Introduction to C++

Programming

UNIT - 1

Brunda G, Dept. of CSE, RIT


Contents
• Introduction to Object Oriented Programming: Software
Evolution, A Look at procedure-oriented programming,
Object-oriented programming paradigm, Basic Concepts of
Object-oriented programming: Objects, Classes, Data
Abstraction and Encapsulation, Inheritance, Polymorphism,
Dynamic Binding, Message passing, Benefits of OOP,
Application of OOP, C++ overview, A simple C++ Program,
Structure of C++ Program (Textbook 1)

Brunda G, Dept. of CSE, RIT 2


Procedure Oriented Programming Language

• Procedural Programming can be defined as a


programming model which is derived from structured
programming, based upon the concept of calling
procedure.
• Procedures, also known as routines, subroutines or
functions, simply consist of a series of computational
steps to be carried out.
• Languages used in Procedural Programming:
FORTRAN, ALGOL, COBOL,
BASIC, Pascal and C.
Brunda G, Dept. of CSE, RIT 3
• In the procedure oriented approach, the problem is viewed as
sequence of things to be done such as reading, calculation and
printing. Procedure oriented programming basically consist of
writing a list of instruction or actions for the computer to follow
and organizing these instruction into groups known as functions.
• A typical structure for procedural programming is shown:

Brunda G, Dept. of CSE, RIT 4


• In a multi-function program, many important data items
are placed as global so that they may be accessed by
all the functions. Each function may have its own local
data.
• Global data are more vulnerable to an inadvertent
change by a function.
• In a large program it is very difficult to identify what
data is used by which function. In case we need to
revise an external data structure, we also need to revise
all functions that access the data. This provides an
opportunity for bugs to creep in.
• Another serious drawback with the procedural approach
is that we do not model real world problems very well.

Brunda G, Dept. of CSE, RIT 5


Brunda G, Dept. of CSE, RIT 6
Some Characteristics exhibited by procedure-oriented
programming are:
• Emphasis is on doing things (algorithms).
• Large programs are divided into smaller programs
known as functions.
• Most of the functions share global data.
• Data move openly around the system from function to
function.
• Functions transform data from one form to another.
• Employs top-down approach in program design.

Brunda G, Dept. of CSE, RIT 7


Object Oriented Paradigm
• Object-oriented programming (OOP) is a programming
paradigm that organizes code around objects, which are
instances of classes that encapsulate data and behavior.
• It is a widely used programming approach that provides a way
to structure and design software systems by modeling real-
world entities as objects

Languages used in Object-Oriented Programming:


Java, C++, C#, Python,
PHP, JavaScript, Ruby, Perl,
Objective-C, Dart, Swift, Scala.
Brunda G, Dept. of CSE, RIT 8
• OOP treats data as a critical element in the program
development and does not allow it to flow freely around
the system.
• It ties data more closely to the function that operate on
it, and protects it from accidental modification from
outside function.
• OOP allows decomposition of a problem into a number
of entities called objects and then builds data and
function around these objects.

Brunda G, Dept. of CSE, RIT 9


• The organization of data and function in object-oriented
programs is shown in fig

The data of an object can be accessed only by the function associated with that object. However,
function of one object can access the function of other objects.
Brunda G, Dept. of CSE, RIT 10
Some of the features of object oriented programming are:
a) Emphasis is on data rather than procedure.
b) Programs are divided into what are known as objects.
c) Data structures are designed such that they characterize
the objects.
d) Functions that operate on the data of an object are ties
together in the data
structure.
e) Data is hidden and cannot be accessed by external function.
f) Objects may communicate with each other through function.
g) New data and functions can be easily added whenever
necessary.
h) Follows bottom up approach in program design.
Brunda G, Dept. of CSE, RIT 11
Brunda G, Dept. of CSE, RIT 12
Brunda G, Dept. of CSE, RIT 13
1. Write a C++ program to find the area and circumference of a
circle

Brunda G, Dept. of CSE, RIT 14


2. Write a C++ program to find the simple interest

Brunda G, Dept. of CSE, RIT 15


Structure of C++ program
• As it can be seen from program above a typical C++
program would contain four sections as shown in the
below fig.. This section may be placed in separate code
files and then compiled independently or jointly.

Brunda G, Dept. of CSE, RIT 16


A simple program to understand the structure of C++ program
using oops
#include <iostream>
#include <cmath>
using namespace std;
class Circle {
public:
double radius;
double calculateArea() {
return M_PI * pow(radius, 2);
}
double calculateCircumference() {
return 2 * M_PI * radius;
}
};
int main() {
Circle circle;
cout << "Enter the radius of the circle: ";
cin >> circle.radius;
cout << "Area of the circle: " << circle.calculateArea() << endl;
cout << "Circumference of the circle: " << circle.calculateCircumference() << endl;
return 0;
Brunda G, Dept. of CSE, RIT 17
}
Class and Objects
• A C++ class is like a blueprint for an object
• A Class is a user-defined data type that has data
members and member functions.
• Data members are the data variables and member
functions are the functions used to manipulate these
variables together, these data members and member
functions define the properties and behavior of the
objects in a Class.
• In the above example of class Car, the data member will
be speed limit, mileage, etc, and member functions can
be applying brakes, increasing speed, etc.
Brunda G, Dept. of CSE, RIT 18
• An Object is an instance of a Class. When a class is
defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is
Defining
allocated. Class and Declaring
•Objects
A class is defined in C++ using the keyword class
followed by the name of the class. The body of the class
is defined inside the curly brackets and terminated by a
semicolon at the end.

Brunda G, Dept. of CSE, RIT 19


Declaring Objects

Brunda G, Dept. of CSE, RIT 20


Accessing Data Members
• The public data members are also accessed in the same way given however
the private data members are not allowed to be accessed directly by the
object.

Brunda G, Dept. of CSE, RIT 21


// C++ program to demonstrate accessing of data members
#include <iostream>
using namespace std;
class Name {
// Access specifier
public:
// Data Members
string stuname;
// Member Functions()
void printname() { cout << "name is:" << stuname; }
};
int main()
{
// Declare an object of class geeks
Name obj1;
// accessing data member
obj1.stuname = "Abhi";
// accessing member function
obj1.printname();
return 0;
}

Brunda G, Dept. of CSE, RIT 22


#include <iostream>
using namespace std;
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
Room room1;
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
Brunda G, Dept. of CSE, RIT 23
}
#include <iostream>
using namespace std;
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
Room room1;
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
Brunda G, Dept. of CSE, RIT 24
}
Data Abstraction and Encapsulation
• Data abstraction focuses on hiding the internal implementation details of an
object and exposing only its essential functionalities.
Encapsulation
• Concept: Encapsulation is the mechanism of bundling data (properties) and
the methods that operate on that data together within a class. It achieves data
abstraction by controlling access to the data using access specifiers (public,
private, protected).
• Implementation in C++:
• private: Members declared as private are accessible only within the class
definition. This hides the data from external code.
• public: Members declared as public are accessible from anywhere in the
program. These typically represent the functionalities users can interact with.
• protected: Members declared as protected are accessible within the class and
by derived classes (inheritance concept).

Brunda G, Dept. of CSE, RIT 25


INHERITANCE
• Inheritance is the process by which objects of one class
acquired the properties of objects of another classes.
• Sub Class: The class that inherits properties from
another class is called Subclass or Derived Class.
• Super Class: The class whose properties are inherited by
a subclass is called Base Class or Superclass.

Brunda G, Dept. of CSE, RIT 26


Brunda G, Dept. of CSE, RIT 27
Polymorphism
• Polymorphism, a Greek term, means the ability to take
more than on form. An operation may exhibit different
behavior is different instances. The behavior depends
upon the types of data used in the operation.
• For example, consider the operation of addition. For two
numbers, the operation will generate a sum. If the
operands are strings, then the operation would produce
a third string by concatenation. The process of making
an operator to exhibit different behaviors in different
instances is known as operator overloading.

Brunda G, Dept. of CSE, RIT 28


Fig illustrates that a single function name can be used to handle different number and
different types of argument. This is something similar to a particular word having several
different meanings depending upon the context. Using a single function name to perform
different type of task is known as function overloading.

Brunda G, Dept. of CSE, RIT 29


Types of Polymorphism
• Compile-time Polymorphism
• Runtime Polymorphism

Brunda G, Dept. of CSE, RIT 30


DYNAMIC BINDING

• Dynamic binding means that the code associated with a


given procedure call is not known until the time of the
call at run time. It is associated with polymorphism and
inheritance. A function call associated with a
polymorphic reference depends on the dynamic type of
that reference.

Brunda G, Dept. of CSE, RIT 31


Message Passing
• An object-oriented program consists of a set of objects that communicate with each
other. The process of programming in an object-oriented language, involves the
following basic steps:
1. Creating classes that define object and their behavior,
2. Creating objects from class definitions, and
3. Establishing communication among objects.

Brunda G, Dept. of CSE, RIT 32


Benefits of OOP
Oop offers several benefits to both the program designer and the user. Object-oriented contributes
To the solution of many problems associated with the development and quality of software
products.
The principal advantages are :
1. Through inheritance we can eliminate redundant code and extend the use of existing classes.
2. We can build programs from the standard working modules that communicate with one another,
rather than having to start writing the code from scratch. This leads to saving of development time
and higher productivity.
3. This principle of data hiding helps the programmer to build secure programs that can’t be
invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist with out any interference.
5. It is easy to partition the work in a project based on objects.
6. Object-oriented systems can be easily upgraded from small to large systems.
7. Message passing techniques for communication between objects makes the interface
description with external systems much simpler.
8. Software complexity can be easily managed.

Brunda G, Dept. of CSE, RIT 33


APPLICATION OF OOP:
The most popular application of oops up to now, has been in the area of user interface design
such as windows. There are hundreds of windowing systems developed using oop techniques.
Real business systems are often much more complex and contain many more objects with
complicated attributes and methods. Oop is useful in this type of applications because it can
simplify a complex problem. The promising areas for application of oop includes.
1. Real – Time systems.
2. Simulation and modeling
3. Object oriented databases.
4. Hypertext,hypermedia and expertext.
5. Al and expert systems.
6. Neural networks and parallel programming.
7. Dicision support and office automation systems.
8. CIM / CAM / CAD system.

Brunda G, Dept. of CSE, RIT 34


a) Write a C++ program to find the factorial of a number

#include <iostream>
using namespace std;
int main() {
int n;
long factorial = 1.0;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't
exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
return 0;
}

Brunda G, Dept. of CSE, RIT 35


Write a C++ program to find the sum of all the natural numbers from 1
to n.

#include <iostream>
using namespace std;

int main() {
int n;
int sum = 0;

cout << "Enter a positive integer (n): ";


cin >> n;
if (n <= 0) {
cout << "Please enter a positive integer." << endl;
} else {
// Calculate the sum of natural numbers from 1 to n
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum of natural numbers from 1 to " << n << " is: " << sum << endl;
}
return 0; // Return with success code 0
} Brunda G, Dept. of CSE, RIT 36
b) Write a C++ program to find whether the entered number is palindrome or not.
#include <iostream>
using namespace std;

int main() {
int num, reversedNum = 0, originalNum, remainder;

cout << "Enter an integer: ";


cin >> num;

originalNum = num;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum) {
cout << originalNum << " is a palindrome." << endl;
} else {
cout << originalNum << " is not a palindrome." << endl;
}
return 0;
Brunda G, Dept. of CSE, RIT 37
}

You might also like