Professional Documents
Culture Documents
Original - PDF 1
Original - PDF 1
PROGRAMMING
OBJECT ORIENTED PARADIGM
Object 2
Object 1
Data Data
Function Function
Object 3
Data
Function
2
OBJECT ORIENTED PROGRAMMING
• Objects have both data and methods
• Objects of the same class have the same data elements and methods
• Objects send and receive messages to invoke actions
3
BASIC TERMINOLOGY
object - An object is an instance of a class which combines both data
and functions together.
Method - an action performed by an object (a verb)
Attribute - description of objects in a class
class - a category of similar objects
- does not hold any values of the object’s attributes
By default all members declared inside a class are private to that class
4
Structure of c++ program
HEADERS
CLASS DECLARATION
MAIN FUNCTION
5
Features of OOP
1. Classes
2. Objects
3. Data encapsulation
4. Data abstraction
5. Inheritance
6. Polymorphism
7. Dynamic binding
8. Message passing
6
Benefits of oop
• OOP offers better implementation
• OOP offers better data security
• OOP offers better code reusability
• OOP offers more flexibility
• User defined data types can be easily
constructed
7
Classes in C++
• A class definition begins with the keyword
class.
• The body of the class is contained within a set
of braces, { } ; (notice the semi-colon).
class class_name
{
…. Any valid identifier
….
….
}; Class body (data member + methods)
8
Class Example
• This class example shows how we can encapsulate (gather) a
circle information into one package (unit or class)
9
class Circle
{
private:
double radius;
public:
Circle() { radius = 0.0;}
Circle(int r);
void setRadius(double r){radius = r;}
double getDiameter(){ return radius *2;}
double getArea();
Scope resolution operator
double getCircumference(); ::
};
Circle::Circle(int r)
{
radius = r; Defined outside class
}
double Circle::getArea()
{
return radius * radius * (22.0/7);
}
double Circle:: getCircumference()
{
return 2 * radius * (22.0/7);
}
10
FUNDAMENTAL OF OOP C++
• #include directive
• main () function
• Class Variables & Functions
• Input, output operators
• Cascading I/O operators
• Comments
• Example C++ program
• Creating source file
• Compile & Execute the c++ programs
11
Input operators
cin >> variable-name;
Meaning: read the value of the variable called
<variable-name> from the user
Example:
cin >> a;
cin >> x;
cin >> my-character;
12
Output operators
cout << variable-name;
Meaning: print the value of variable <variable-name> to
the user
cout << “any message “;
Meaning: print the message within quotes to the user
cout << endl; Meaning: print a new line
Example:
cout << a;
cout << “This is my character: “ << my-character
<< “ end ” << endl;
Example
13
Difference b/w c and c++
C Language C++ Language
Procedural Programming Language Object Oriented Programming Language
14
Tokens, Keywords and identifiers
• Tokens , a smallest individual units.
• Keywords, identifiers, constants,..etc
• Keywords implements specific c++ language
features
• Identifiers refers the names of variables,
functions, arrays..etc
15
data types
Datatypes
User
defined: Derived:
Class Built-in Array
Structure Pointers
Union Function
Enum
16
STRUCTURE
17
STRUCT EXAMPLE
#include <iostream.h> cin >> p1.age;
struct person cout << "Enter salary: ";
{ cin >> p1.salary;
char name[50]; cout << "\nDisplaying Information." << endl;
int age; cout << "Name: " << p1.name << endl;
float salary; cout <<"Age: " << p1.age << endl;
}; cout << "Salary: " << p1.salary;
return 0;
int main() }
{
person p1;
cout << "Enter Full name: ";
cin>>p1.name;
cout << "Enter age: ";
18
19
BUILT-IN Datatypes
20
Array
• Array is a collection of elements of same type
referred using a unique name. Each element of
an array are stored in memory locations
indexed from 0 through n number of its
elements.
• The lowest indexed will be the first element
and highest indexed the last element of an
array.
• type array_name[array_size_1]
• int age[5];
21
EXAMPLE
#include <iostream.h>
void main()
{
int i;
float mark[6];
cout << "Enter the marks of your 6 subjects:: ";
for(i=0; i<6; i++)
{
cin >> mark[i];
}
float sum=0;
for(i=0;i<6;i++)
{
sum += mark[i];
}
float ave = sum/6;
cout << "Average Marks is::" << ave;
}
22
OPERATORS
23
OPERATORS
Arithmetic operators Relational
Operators Operat Meaning Example Result value
or
operators met Example < Less Than x=10,y=5 False 0
+ Addition x=10,y=5 X<y
x+y -> 21
> Greater Than X>y True 1
- Subtraction X-y -> 5
* Multiplication X*y -> 50 <= Less than or X<=y False 0
/ Division x/y -> 2
Equal to
% Modulo Division X%y -> 0 >= Greater than or X>=y True 1
equal to
!= Not Equal to X!=y False 0
== Equal To X==y False 0
24
Control structures
• if
• if else
• Nested if
• switch
• do while
• while
• for
25