Вы находитесь на странице: 1из 57

Object Oriented

Programming Language

KGiSL

Programming Language
2

is

an artificial language designed to express


computations that can be performed by a machine,
particularly a computer.

a series of instructions written by a programmer according

to a given set of rules or conventions (syntax ).

is a notation for writing programs.


All PL language can be divided into three categories

KGiSL

Problem Oriented or High Level Languages


Machine Oriented or Low Level Languages
Middle Level Language

Programming languages
High-level languages

To speed up programming even further Single statements


for accomplishing substantial tasks

Translator programs called Compilers to convert


high-level programs into machine language

E.g. add overtime to base pay and store result in gross pay
grossPay = basePay + overtimePay

KGiSL

Types of Programming Language


4

Elementary

Advanced

Procedural

Procedural

Structure Oriented

Object

Advanced

Oriented

Object
Oriented

Eiffel

Fortran

Algol 68

C++

C#

Cobol

PL/I

Pascal

Ada 95

Java

Ada 83

KGiSL

What is C++?
5

C++ is an object-oriented programming (OOP)

language that is viewed by many as the best


language for creating large-scale applications.
C++ is a multi paradigm language.
C++ is an object driven language.

KGiSL

Introduction to C++
6

C++ (pronounced see plus plus) was

developed by Bjarne Stroustrup at Bell Labs in


1979.
Combination of Simula67 and c
C With classes
In 1983,the name was changed to C++.
C++ is an extended version of C.
C++ is a superset of C.
All the concepts of C are applicable to C++
also.
KGiSL

Variables
7

Variables are like small blackboards


We can write a number on them
We can change the number
We can erase the number

C++ variables are names for memory


locations
We can write a value in them
We can change the value stored there
We cannot erase the memory location

KGiSL

Some value is always there

Identifiers
8

Variables names are called identifiers


Choosing variable names
Use meaningful names that represent data to be
stored
First character must be

a letter
the underscore character

Remaining characters must be

KGiSL

letters
numbers
underscore character

Keywords
9

Keywords (also called reserved words)

Are used by the C++ language

Must be used as they are defined in the programming


language

Cannot be used as identifiers

Keyword is an essential part of a language definition.

KGiSL

Types of C Constants
10

C++ constants can be divided into two major

categories

Primary Constants
Secondary Constants
C Constants
Primary Constants
Integer Constant
Real Constant
Character Constant

KGiSL

Secondary constants

Array, Pointer
Structure, Union
enum

Data Types
11

The most commonly used Data Types in C++


programming language:

KGiSL

short int
int
long int
float
double
long double
char
bool

Basic Structure of C++ Program


12

Include files
Global variable and
function declaration/
Function Definition
main function
Function or subprogram
KGiSL

Simple C++ Program


13

Comment

m
ty ain
pe r
in etu
te rn
ge s
r of

/* A first C++ Program*/


#include<iostream.h>
int main()
Output
Stream
{

Includin

Fun
ctio
nm

g h eade

r files

ain

Insertion operator

Statement terminated
with ;

cout<<Learning C++ Programming is easy \n";


Ret
return 0;
u
String to print
suc rn 0 o
n
ces
}
s
ful e
xec
utio
One way to exit the function
n
or to return something

KGiSL

Execution of C++ Program


14

Phase 1

Editor

Program edited in

Disk

Editor and stored


on disk
Preprocessor

Phase 2

Preprocessor

Disk

program processes
the code

Phase 3

Phase 4

KGiSL

Compiler
Linker

Creates object code

Disk

and stores on disk

Links object code

Disk

with libraries and


stores on disk

Execution of C++ Program


15
Primary memory

Phase 5

Puts program in

Loader

memory

Primary memory

Phase 6

CPU

Takes each instruction


and executes it storing
new data values

KGiSL

Comment
16

Comment should be enclosed between /*

*/

Single Line Comment is //


It is used to increase the readability of the program.
Any number of comments can be given at any place in the

program.
Comment cannot be nested.
It can be split over more than one line

KGiSL

Operators in C++
17

An operator is a symbol (Eg:+,-,*,/)

that directs the computer to perform


certain mathematical or logical
manipulations and is usually used to
manipulate data and variables.
Eg: a + b

KGiSL

Types
18

1.
2.
3.
4.
5.
6.
7.
8.

KGiSL

Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators

Control Structures
19

Control structures help to specify the order in


which actions execute.

The type of control structure are

KGiSL

Conditional Statements

Iteration Statements

Selective Statements

Jump Statements

ARRAYS
20

An array is a collection of variables of similar data type.


A specific element in an array is accessed by an index.
Arrays may have from one to several dimensions.
In C/C++, all arrays consist of contiguous memory

locations.

Integer array of 4 elements


1000
1002
1004
1006
The lowest address corresponds to the first element and
the highest address to the last element.

KGiSL

FUNCTIONS
21

Function is a basic requirement of modular

programming.
The process of dividing a large program into small

sub programs and manipulating them independently


is known as modular programming.
A function is a named, independent, block of

statements that perform well defined, specific


task and may return a value.
KGiSL

Structure of Function
22

<return type> <function name>(arg 1,


arg 2,,arg n)
{
declaration part;
statements;
return;
}
KGiSL

Input and Output


23

A data stream is a sequence of data

Typically in the form of characters or numbers

An input stream is data for the program to use

Typically originates

at the keyboard
at a file

An output stream is the programs output

Destination is typically

KGiSL

the monitor
a file

Output using cout


24

cout is an output stream sending data to the monitor


The insertion operator "<<" inserts data into cout
Example:
cout << number_of_students << " Students\n";

This line sends two items to the monitor

The value of number_of_students


The quoted string of characters " Students\n
The

\n causes a new line to be started following the


s in Students

KGiSL

A new insertion operator is used for each item of output

Input Using cin


25

cin is an input stream bringing data from the keyboard


The extraction operator (>>) gets data to be used
Example:
cout<<"Enter the number of students in a class\n;
cin >> number_of_students;
This code prompts the user to enter data then

read one data items using cin

The value read is stored in number_of_students

KGiSL

Reading Data From cin


26

Multiple data items are separated by spaces


Data is not read until the enter key is

pressed
Example:
cin >> v1 >> v2 >> v3;
Requires three space separated values
User might type
34 45 12 <enter key>

KGiSL

Designing Input and Output


27

Prompt the user for input that is desired

cout statements provide instructions

cout << "Enter your age: ";


cin >> age;
Displaying the output what was read

cout << age << " was entered." <<


endl;

KGiSL

Basic OOPS Concepts


28

KGiSL

What is Object Oriented Programming?


29

Identifying objects and

An object is like a
black box.
The internal details
are hidden.

KGiSL

assigning responsibilities to
these objects.
Objects communicate to other
objects by sending messages.
Messages are received by the
methods of an object

What is an object?
30

Tangible Things
Roles
Incidents
Interactions
Specifications

KGiSL

as a car, printer, ...


as employee, boss, ...
as flight, overflow, ...
as contract, sale, ...
as colour, shape,

So, what are objects?


31

an object represents an individual, identifiable

item, unit, or entity, either real or abstract, with a


well-defined role in the problem domain.
Or
An "object" is anything to which a concept
applies.
Etc.

KGiSL

Why do we care about objects?


32

Modularity - large software projects can be

split up in smaller pieces.


Reusability - Programs can be assembled
from pre-written software components.
Extensibility - New software components can
be written or developed from existing ones.

KGiSL

Classes
33

A class is grouping of objects having identical

properties, common behavior and shared


relationship.
The entire group of data and code of an object can

be built as a user-defined data type using class.


Objects are variables of a type class.
A class is a model of the object.

KGiSL

34

class A
{
private:
data member1;
data member2;
data member(n);
public:
method1();
method2();
};
KGiSL

Example: The Person class


35

#include<string.h>
#include<iostream.h>
class Person{
char name[20];
int yearOfBirth;
public:
void displayDetails() {
cout << name << " born in "
<< yearOfBirth << endl;
}
//...
};
KGiSL

private
data

public
processes

The two parts of an object


36

Object = Data + Methods


or to say the same differently:
An object has the responsibility to know and the
responsibility to do.

=
KGiSL

Behaviour and Messages


37

The most important aspect of an object is its

behaviour (the things it can do). A behaviour is


initiated by sending a message to the object (usually
by calling a method).

KGiSL

The two steps of Object Oriented Programming


38

Making Classes: Creating, extending or reusing

abstract data types.


Making Objects interact: Creating objects from

abstract data types and defining their relationships.

KGiSL

Basic Terminology
39

Abstraction is the representation of the essential

features of an object. These are encapsulated into


an abstract data type.
Encapsulation is the practice of including in an
object everything it needs hidden from other
objects. The internal state is usually not accessible
by other objects.

KGiSL

Example
#include <iostream.h>
#include <cstring.h>

40

class employee {
char name[80]; // private by default
public:
void putname(char *n); // these are public
void getname(char *n);
private:
double wage; // now, private again
public:
void putwage(double w); // back to public
double getwage();
};
KGiSL

void employee::putname(char *n)


{
strcpy(name, n);
41
}
void employee::getname(char *n)
{
strcpy(n, name);
}
void employee::putwage(double w)
{
wage = w;
}
double employee::getwage()
{
return wage;
}

KGiSL

42

int main()
{
employee ted;
char name[80];
ted.putname("Ted Jones");
ted.putwage(75000);
ted.getname(name);
cout << name << " makes $";
cout << ted.getwage() << " per year.";
return 0;
}
KGiSL

Friend
43

Friend function
Friend Class

A friend function and friend class (and all of its member


functions) has access to all private and protected
members of the class for which it is a friend.

KGiSL

Example
44

#include <iostream>
using namespace std;
class myclass {
int a, b;
public:
friend int sum(myclass x);
void set_ab(int i, int j);
};
void myclass::set_ab(int i, int j)
{
a = i;
b = j;
}
KGiSL

// Note: sum() is not a member function of any class.


45
int sum(myclass x)
{
/* Because sum() is a friend of myclass, it can
directly access a and b. */
return x.a + x.b;
}
int main()
{
myclass n;
n.set_ab(3, 4);
cout << sum(n);
return 0;
}

KGiSL

Polymorphism
46

Polymorphism means having many forms. It

allows different objects to respond to the same


message in different ways, the response specific to
the type of the object.
Function

Overloading

Operator

Overloading

KGiSL

Example
47

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float ,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:"<<pi*a*a;
}
KGiSL

48

void fn::area(int a,int b)


{
cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,int a,int b)
{
cout<<"Area of triangle:"<<t*a*b;
}
void main()
{
int ch;
int a,b,r;
clrscr();
fn obj;
KGiSL

49

cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle\n2.Area of
Rectangle\n3.Area of Triangle\n4.Exit\n:;
cout<<Enter your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radious of the Circle:";
cin>>r;
obj.area(r);
break;
KGiSL

50

case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}
KGiSL

Inheritance
51

Inheritance means that one class inherits the

characteristics of another class.


This is also called a is a relationship:
A car is a vehicle
A dog is an animal
A teacher is a person

KGiSL

52

Inheriting the parent's nature


Red

Orange

KGiSL

Yellow

Blue

Green

Violet

Example
53

#include <iostream>
using namespace std;
// Base class
class Shape
{
public:
void setWidth(int w) { width = w; }
void setHeight(int h) { height = h; }
protected:
int width;
int height;
};
KGiSL

54

// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};

KGiSL

55

int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea();
return 0;
}

KGiSL

Types of Inheritance
56

Single

Multi Level

A
B

Hierarchal

Multiple
A

Hybrid

A
C

B
D

THANK
57

U
KGiSL

Вам также может понравиться