Sei sulla pagina 1di 47

C++ -Object Oriented Programming

KGiSL

Introduction to C++

C++ is an object oriented programming language.

It is an extension of c.
It was developed by Bjarne Stroustrup. Combination of more than one programming language. It is mainly used for creating complicated applications.

KGiSL

Page 2

Evolution of C++ The combination Simula67 and C.

It was named as C with classes.


In 1983,the name was changed to C++. C++ is an extended version of C. C++ is a superset of C.

KGiSL

Page 3

Programming Paradigms

A programming paradigm is a model of programming based on distinct concepts that shapes the way programmers design, organize and write programs. - Monolithic Programming - Procedural Programming - Structured Programming

KGiSL

Page 4

Monolithic Programming

- Monolithic Programming indicates the program which contain a single function for the large program. - Monolithic programming will not divide the program and it is a single thread of execution. - In monolithic programming data variables are declared globally and the statements are written in sequence.

- The program contains jump statements such ad goto.

KGiSL

Page 5

Procedural Programming

- A procedural programming language is one where programs are organized into blocks of code called variously "subroutines", "functions", or "procedures", each of which handles one particular task. - FORTRAN and COBOL

KGiSL

Page 6

Structured Programming

- A computer programming technique that follows a top down design approach with block oriented structures. - In structured programming languages such as PASCAL and C larger programs are developed. - Programs are divided into multiple sub modules and procedures. - Each module has its own set of local variable. - User-defined data types are introduced.
KGiSL

Page 7

Object Oriented Technology

Object-oriented programming (OOP) is a programming paradigm that uses "objects" data structures consisting of data fields and methods together with their interactions to design applications and computer programs.

KGiSL

Page 8

KEY CONCEPTS OF OBJECT-ORIENTED PROGRAMMING

There are several fundamental or key concepts in object-oriented programming. 1. 2. 3. 4. 5. 6. 7. 8. Classes Objects Method Data Abstraction Encapsulation Inheritance Polymorphism Reusability

KGiSL

Page 9

Objects

Objects are primary run-time entities in object-oriented programming. An object is a real world thing or concept expressed as a software representation. Hardware,software,documents,human beings and concepts are example of objects.

Objects are instance of a class.

KGiSL

Page 10

Classes

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

Page 11

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

Members

Methods or Member functions

KGiSL

Page 12

Objects and their properties

Class : car Properties : company,model,color and capacity. Actions : speed(),average() and break() Class : computer Properties : brand,price,monitor,hard disk Actions : processing(),display() and printing()

KGiSL

Page 13

Example

Emp1 Class Employee Employee (Real World) Emp2 Emp3 . . . Emp n

Empid Name Address

KGiSL

Page 14

Method

Method is a set of procedural statements for achieving the desired result. It performs different kinds of operations on different data types.

It is also known as functions.


Class : computer Properties : brand,price,monitor,hard disk Actions : processing(),display() and printing()

KGiSL

Page 15

Data Abstraction

Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes.

KGiSL

Page 16

Encapsulation

The packing of data and functions into a single component is known as encapsulation. C++ supports the features of encapsulation using classes. The data is not accessible by outside functions. Datas can be accessed only the functions defined inside the class.

Data hiding can be accomplished with encapsulation.


By data hiding an object can be used by the user without knowing how it works internally.
KGiSL
Page 17

Inheritance

Inheritance is an important and powerful concept in object oriented technology. Inheritance is the method by which objects of one class get the properties of objects of another class. It means,existing class can be used to create a new class. New class is known as derived class and the existing class is known as base class.

KGiSL

Page 18

Inheritance

Red

Yellow

Blue

Orange

Green

Violet

KGiSL

Page 19

Polymorphism
Polymorphism allows the same function name to act differently in different classes. It is an important feature in OOP concept and has the ability to take more than one form.

KGiSL

Page 20

Difference b/w C and C++

C
C- Procedure Oriented C does not have any classes or objects. C input/output is based on library and the processes are carried out by including functions. C functions do not support overloading. C does not support new or delete commands.

C++ Object Oriented It supports Classes and Objects

C++ i/o is made through console commands cin and cout.

C++ supports Overloading. New keyword is used to create an object for a class.

KGiSL

Page 21

Difference b/w C and C++(Contd)

C has a top down approach .

c++ has a bottom up approach.

In c, declaring the global variable several times is allowed.


C has predefined data types.

In C++, It is not allowed .

In c++,User can creates its own datatype using class.

KGiSL

Page 22

Structure of a Program

// First Program #include <iostream.h> int main () { cout << Welcome to KGiSL!"; return 0; }

KGiSL

Page 23

Structure of a Program(Contd) main() - The c++ program is a collection of functions. - Every program must have a main(). Comments - C++ introduces a new comment symbol //(double slash) - Multiline comment /* line1 line2 line3 */

KGiSL

Page 24

Input and Output in C++

KGiSL

Page 25

Variables,Constants and Data types in C++ Variables - A variable is the storage location in memory that is stored by its value. - A variable is identified or denoted by a variable name. - The variable name is a sequence of one or more letters, digits or underscore, for example: character _

KGiSL

Page 26

Rules for defining variable name


A variable name can have one or more letters or digits or underscore for example character _. White space, punctuation symbols or other characters are not permitted to denote variable name. . A variable name must begin with a letter. Variable names cannot be keywords or any reserved words of the C++ programming language. C++ is a case-sensitive language. Variable names written in capital letters differ from variable names with the same name but written in small letters . For example, the variable name ID differs from the variable name id.
KGiSL

Page 27

Data types in C++

The most commonly used Data Types in C++ programming language: - short int - int - long int - float - double - long double - char

KGiSL

Page 28

Declaration of variable

The syntax for declaring variable name is:datatype variable_name; Example: int a; If there exists more than one variable of the same type, such variables can be represented by separating variable names using comma. For instance int x,y,z ;

KGiSL

Page 29

Operators in C++

The operators available in C++ programming language are:

Assignment Operator denoted by = Arithmetic operators denoted by +, -, *, /, % Compound assignment Operators denoted by +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= Increment and Decrement operator denoted by ++, - Relational and equality operators denoted by ==, !=, >, <, >=, <= Logical operators denoted by !, &&, || Conditional operator denoted by ? Comma operator denoted by ,
KGiSL
Page 30

Control Structures Conditional Structures Iteration Structures Selective Structure

KGiSL

Page 31

Conditional Structure

- It is also known as Decision Making Statements. - C++ has the following statements if statement ifelse statement Else if statement

KGiSL

Page 32

if statement:
syntax: if ( condition ) statement;

KGiSL

Page 33

Example for simple if

#include<iostream.h> #include<conio.h> void main() { int x=10,y=20; if(x<y) cout<<x is less than y; getch(); }

KGiSL

Page 34

If.else statement

Syntax:
if ( condition ) { // Execute these statements if TRUE } else { // Execute these statements if FALSE }
KGiSL
Page 35

Example for ifelse


#include<iostream.h> #include<conio.h> void main() { int x,y; cout<<Enter value of x:; cin>>x; cout<<Enter value of y:; cin>>y; if(x>y) { cout<<x is greater; } else { cout<<y is greater; } getch(); }

KGiSL

Page 36

ifelse if statement:
Syntax:

if(condition) { statement1; } else if(condition) { statement2; } else { statement; }

KGiSL

Page 37

Example for else if statement


#include<iostream.h> #include<conio.h> void main() { int a,b,c; cout<<Enter value of a:; cin>>a; cout<<Enter value of b:; cin>>b; cout<<Enter value of c: cin>>c; if(a>b && a>c) { cout<<a is greater; } else if(b>a && b>c) { cout<<b is greater; } else { cout<<c is greater; } getch(); }

KGiSL

Page 38

Iteration Structure

- A section of code in which an instruction or group of instructions is executed a specific number of times depending on the value of a loop counter. for loop while loop dowhile loop

KGiSL

Page 39

for loop
Syntax:

for (initialization; condition; increment/decrement) { statement; }

KGiSL

Page 40

Example of for loop

#include <iostream.h> int main() { int count; for(count=1; count <= 100; count=count++) cout << count << " "; return 0; }

KGiSL

Page 41

While loop

The while loop allows programs to repeat a statement or series of statements, over and over, as long as a certain test condition is true. The while loop is an entry-condition loop. Syntax: while (test_condition) { statement; }

KGiSL

Page 42

Example
#include<iostream.h> void main() { int num = 0; while (num < 10) { cout << "*"; num++; } }

KGiSL

Page 43

Dowhile The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. The do-while loop is an exit-condition loop. Syntax do { statement; } while (condition);

KGiSL

Page 44

Example
#include<iostream.h> #include<conio.h> void main() { clrscr(); int num=0; do { num++; cout<<num; }while(num<5); getch(); }

KGiSL

Page 45

switch

- switch Statement Its objective is to check several possible constant values for an expression. Syntax: switch (expression) { case 1: group of statements 1; break; case 2: group of statements 2; break;. default: default group of statements }
KGiSL
Page 46

Example
#include <iostream.h> #include<conio.h> void main() { int color ; clrscr(); cout<< Colors; cout<< ******; cout<<1.Red 2.Green 3.Blue; cout<<"Enter an integer to choose a color:; cin>>color;
switch(color)

{ case 1:

cout<<"you chose red color\n"; break;


case 2: cout<<"you chose green\n"; break;

case 3:
cout<<"you chose blue color; break; default: cout<<Wrong choice"; } getch(); }

KGiSL

Page 47

Potrebbero piacerti anche