c++ workshop ppt
DESCRIPTION
An OverviewTRANSCRIPT
KGiSL KGiSL
C++ -Object Oriented Programming
Page 2 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.
Page 3 KGiSL
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.
Page 4 KGiSL
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
Page 5 KGiSL
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.
Page 6 KGiSL
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
Page 7 KGiSL
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.
Page 8 KGiSL
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.
Page 9 KGiSL
KEY CONCEPTS OF OBJECT-ORIENTED PROGRAMMING
There are several fundamental or key concepts in object-oriented programming.
1.Classes2.Objects3.Method4.Data Abstraction5.Encapsulation6.Inheritance7.Polymorphism8.Reusability
Page 10 KGiSL
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.
Page 11 KGiSL
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.
Page 12 KGiSL
class A
{
private:
data member1;
data member2;
data member(n);
public:
method1();
method2();
};
Members
Methods or Member functions
Page 13 KGiSL
Objects and their properties
Class : carProperties : company,model,color and capacity.Actions : speed(),average() and break()
Class : computerProperties : brand,price,monitor,hard diskActions : processing(),display() and printing()
Page 14 KGiSL
Example
Employee(Real World)
Class Employee
EmpidName
Address
Emp1
Emp2
Emp3...
Emp n
Page 15 KGiSL
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 : computerProperties : brand,price,monitor,hard diskActions : processing(),display() and printing()
Page 16 KGiSL
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.
Page 17 KGiSL
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.
Data’s 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.
Page 18 KGiSL
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.
Page 19 KGiSL
Inheritance
Red Yellow
Orange
Blue
VioletGreen
Page 20 KGiSL
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.
Page 21 KGiSL
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.
Page 22 KGiSL
C has a top down approach .
In c, declaring the global variable several times is allowed.
C has predefined data types.
c++ has a bottom up approach.
In C++, It is not allowed .
In c++,User can creates its own datatype using class.
Difference b/w C and C++(Contd…)
Page 23 KGiSL
Structure of a Program
// First Program#include <iostream.h> int main () {
cout << “Welcome to KGiSL!"; return 0;
}
Page 24 KGiSL
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 */
Page 25 KGiSL
Input and Output in C++
Page 26 KGiSL
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 _
Page 27 KGiSL
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.
Page 28 KGiSL
Data types in C++
The most commonly used Data Types in C++ programming language:- short int - int - long int - float - double - long double - char
Page 29 KGiSL
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 ;
Page 30 KGiSL
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 ,
Page 31 KGiSL
Control Structures
Conditional Structures Iteration StructuresSelective Structure
Page 32 KGiSL
Conditional Structure
- It is also known as Decision Making Statements.- C++ has the following statements
if statement if…else statement Else if statement
Page 33 KGiSL
if statement:
syntax:if ( condition )
statement;
Page 34 KGiSL
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();
}
Page 35 KGiSL
If….else statement
Syntax:
if ( condition ) { // Execute these statements if TRUE } else { // Execute these statements if FALSE }
Page 36 KGiSL
Example for if…else
#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();
}
Page 37 KGiSL
if…else if statement:Syntax:
if(condition)
{
statement1;
}
else if(condition)
{
statement2;
}
else
{
statement;
}
Page 38 KGiSL
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();}
Page 39 KGiSL
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 do…while loop
Page 40 KGiSL
for loop
Syntax:
for (initialization; condition; increment/decrement){ statement; }
Page 41 KGiSL
Example of for loop
#include <iostream.h> int main() { int count; for(count=1; count <= 100; count=count++) cout << count << " "; return 0; }
Page 42 KGiSL
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;
}
Page 43 KGiSL
Example
#include<iostream.h>
void main()
{
int num = 0; while (num < 10)
{
cout << "*";
num++;}
}
Page 44 KGiSL
Do…whileThe 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. Syntaxdo{ statement; } while (condition);
Page 45 KGiSL
Example
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num=0;
do
{
num++;
cout<<num;
}while(num<5);
getch();
}
Page 46 KGiSL
switch
- switch StatementIts 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
}
Page 47 KGiSL
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();
}