C++ theory

9,079 views 113 slides Dec 26, 2016
Slide 1
Slide 1 of 113
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113

About This Presentation

Detail description about C++


Slide Content

C++ Theory

Object Oriented Programming language

C++ is an extension to C Programming language. It was developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup . It is a deviation from traditional procedural languages in the sense that it follows object oriented programming (OOP) approach which is quite suitable for managing large and complex programs. Some history about of c++

Inheritance Polymorphism Encapsulation Oop Concepts. . .

The capability of one class to inherit properties from another class as a child inherits some properties from his/her parents. The most important advantage of inheritance is code reusability. Once a base class is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Reusing existing code saves time, money and efforts of writing the code again. Without redefining the old class, you can add new properties to desired class and redefine an inherited class member function. inheritance

inheritance vehicle 2 wheeler 4 wheeler honda Yamaha tata mahindra

Encapsulation is the most basic concept of OOP. It is the way of combining both data and the functions that operate on that data under a single unit. The only way to access the data is provided by the functions (that are combined along with the data). These functions are considered as member functions in C++. It is not possible to access the data directly. If you want to reach the data item in an object, you call a member function in the object. It will read the data item and return the value to you. The data is hidden, so it is considered as safe and far away from accidental alternation. Data and its functions are said to be encapsulated into a single entity. Encapsulation

Encapsulation Private data Public data Calling object

Polymorphism is a key to the power of OOP. It is the concept that supports the capability of data to be processed in more than one form. Polymorphism

pollymurphism What is your name??? amit sanjay prakash

Pre processing directive. Header files Iostream.h Input output stream header file. Containing Cin and cout menthod #include

C++ is totally case sensitive language. Important note

cout <<“ welcome ”; cout = method << = operator “welcome “ = content to write ; = statement termination Writing on screen

Int k; cin >>k; cin = method >> = operator k = variable name to store data ; = statement termination Reading the data from keyboard

int I,j,k,l,m ; cin >>I>>j>>k >>l>>m ; On Screen : 10 20 30 40 50 On memory i = 10 j = 20 k = 30 l = 40 m = 50 Reading multiple values

#include< iostream.h > #include< conio.h > #include< stdio.h > Void main() { Clrscr (); Cout <<“my first program in c++ ”; Getch (); } My first program

Registered and reserved words of c++ are known as a key word. Not use to create variables. Key word

List of Keywords asm double new switch auto else operator template break enum private this case extern protected try catch float public typedef char for register union class friend return unsigned const goto short virtual continue if signed void default inline sizeof volatile delete int static while do long struct List of registered keywords of c++

Brackets [ ] opening and closing brackets indicate single and multidimensional array subscript. Parentheses ( ) opening and closing brackets indicate functions calls, function parameters for grouping expressions etc. Braces { } opening and closing braces indicate the start and end of a compound statement. Comma , it is used as a separator in a function argument list. Semicolon ; it is used as a statement terminator. Colon : it indicates a labelled statement or conditional operator symbol. Asterisk * it is used in pointer declaration or as multiplication operator. Equal sign = it is used as an assignment operator. Pound sign # it is used as pre-processor directive. Punctuators

Char Integer Float Double Boolean Data types

If condition If else condition If else if condition Switch case Break statement Continue statement Control statement

If(condition) { Executable part } If condition

If(condition) { execute when condition ins true } else { execute when condition is false } If else

If(condition 1) { execute if condition is true } else if( condition 2) { execute when condition 1 is false and condition 2 is true } If else if

Switch(parameter) { Case (condition): { executable part; Break; } Case (condition): { executable part; Break; } } Switch case

Looping structure

While(condition) { Executable part Increment / decrement; } While loop

Do { Executable part; Increment / decrement }while(condition); Do while loop Note : do while loop compulsory execute once

For( initialization;condition;increment /decrement) { Executable part } For loop

Arithmetic Logical Conditional Relational Increment / decrement Special Assignment Urinary / short hand Operator

+ ( summation ) - ( subtraction ) / ( division ) * ( multiplication ) % ( modulation ) Arithmetic

|| ( or sign ) && ( and sign ) ! ( not sign ) Logical

Condition 1 Condition 2 Result True False False False True False False False False True True True And (&&) operator ( flow chart)

Condition 1 Condition 2 Result True False True False True true False False False True True True Or (||) operator ( flow chart)

Condition 1 condition 2 Result True False False False True False False False True True True False Not ( ! ) operator flow chart

> ( greater than) < ( less than) >= ( greater than equal) <= ( less than equal ) == ( equals to ) != ( not equals ) Relational

(condition? true: false) conditional Special operator , ( comma operator ) Ex. int a, b, c;

++ -- Pre increment / decrement ++ Variable name -- variable name Post increment / decrement variable name ++ variable name -- Increment / decrement

Variable name += Variable name +- Variable name +/ Variable name +* Variable name +% Compound assignment

Ex cout <<“string 1” << “string 2” ; Concatination

All the statement of c ++ are terminated by ; (semi colon ) Must remember

String functions Math functions Character functions Inbuilt functions

Header file String.h Functions strlen strrev strcpy strcmp strupr strlwr String functions

Header file math.h Min Max Round Sqrt Abs Floor Ceil Math functions

Header file Ctype.h Getch Getche Isdigit Isalpha Isalnum Isspace Character functions

Define in main four type. No parameter no return value No parameter with return value With parameter no return With parameter with return User define functions

Abc () Void Abc () { Executable part } No parameter no return value

Int I = abc (); Int abc () { return 5; } note : value of I = 5 No parameter with return

Abc (5,10); Void Abc ( a,b ) { Cout <<5+10; } With parameter no return

Int I = abc (5,10); Int abc ( a,b ); { Return a + b; } Value of I = 15. With parameter with return value

Single dimention Multi dimention Collection of same data type. Int a[0]; Array

Index of array start with 0 (zero) Array cont….. Index 10 A[0] A[1] A[2] A[3] 20 30 40 Int A[4] value

Ex. Int a[4][3] 4 rows 3 columns Multi dimension array a[1] 40 50 60 a[2] 70 80 90 a[3] 100 110 120 a[0] 10 20 30 index a[0] a[1] a[2] a[1][1] = 50

User define data type Key word Enum use to store only user define data enum day = {“Sunday” , ”Monday” , ”Tuesday”} Index start from 0 Ex index of Sunday is 0. Enumerated data type

Collection of different data type User define data type Access by . (Dot) Define with struct keyword structure

struct structure name { Variable data type variable name Variable data type variable name Variable data type variable name Up to n variables. . . }; syntax

struct std { int rn ; float per; }; Void main() { std s; S.rn = 915; } example

Use to define method and scope of method Use :: to define scope Define methods in class and out side of class also. Scope resolution operator

Return type class name :: method name( list of parameters) Syntax

class abc { Public: Void msg (); }; void abc :: msg () { Executable part } example

Class : Collection of methods and variables. Collection of private and public data. Object : Instants that Use to access functionality of class. Class and object

class class_name { Public : Methods Variables }; Class

Return type method name ( list of parameters) { Executable part [ return data ] }; Method

Class name object name; Ex. abc a; Class name abc Object name a Creating object

Create memory space on RAM. Calling when creating object of any class All class contains 1 default constructor. Constructor name and class name both are same. Default constructor not take any parameters. Constructor

About constructor A constructor is a special member function that initializes the objects of its class. It is special because its name is the same as the class name. It is invoked automatically whenever an object is created. It is called constructor because it constructs the values of data members of the class. It does not have any return type, not even void.

class abc { public: abc () { Executable part } }; Constructor example

Use to delete object of any class Call by ~ sign. Destructor never return any kind of values. Destructor

class abc { public : ~ abc () { } } Destructor example

class abc { public : void msg () { cout <<“welcome ”; }; }; void main { abc a; a.msg(); } o/p = welcome Accessing method through object

Re usability of any class Extends on class in another class Code re usability Access methods and variables of super class Inherit functionality of super class. Use of pre define class inheritance

Mainly 5 type of inheritance available in c++ Single level inheritance Multi level inheritance Multiple inheritance Hierarchical inheritance Hybrid inheritance Type of Inheritance in c++

Base class Derived class Access public private protected specifier public public private protected private Not Not Not inherited inherited inherited protected protected protected protected Mode of inheritance

Ex. Functionality of class A use in class B Single level inheritance A B

Multi level inheritance a b c d

Multiple inheritance c a b

Hierarchical inheritance a b c d

Combination of hierarchical and multiple Hybrid inheritance a d b b

Remember Available types of inheritance is Single level inheritance Multi level inheritance Multiple inheritance Hierarchical inheritance Hybrid inheritance Example and syntaxes of inheritance

Ex. Functionality of class A use in class B Single level inheritance A B

Base Class name : mode of inheritance super class name Different Modes Public Private Protected Syntax of single level inheritance

class abc { Methods and variable }; class xyz : public abc { Methods and variable }; Single level inheritance example

Multi level inheritance a b c d

Class name (parent) { } Class name(child):access specification class name( parent class ) { } Class name (sub child) : access specification class name (parent class) Syntax of multilevel inheritance

Class demo { } Class demo1: public demo { } Class demo2 : public demo1 { } example

Multiple inheritance c a b

Class class name { } class class name { } class class name : access specification class name ( parent 1 ), (parent 2 ) Syntax of multiple inheritance

Class demo { } Class demo1 { } Class final : public demo, public demo1 { } Example

Hierarchical inheritance a b c d

Class class name { } Class calss name : access specification class name { } Class class name : access specification class name { } Hierarchical inheritance syntax

Class demo { } Class demo1 : public demo { } Class demo2 : public demo { } Example

Combination of hierarchical Hybrid inheritance a d b b

Class class name { } Class class name : access specification class name { } Class class name : access specification class name { } Class class name : access specification class name, access specification class name { } Hybrid inheritance syntax

Class demo {} Class demo1 : public demo {} Class demo2 : public demo {} Class final : public demo1, public demo2 {} examle

A file is a collection of logically related records. A program usually requires two types of data communication. File handling

File Handling in c ++ Data Type Description ofstream This data type represents the output file stream and is used to create files and to write information to files. ifstream This data type represents the input file stream and is used to read information from files. fstream This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

( i ) Writing data on the datafile : The data flows from keyboard to memory and from memory to storage device. Keyboard —> memory —> hard disk/ floppydisk (ii) Reading data from datafile : The data flows from storage device to memory and from memory to output device, particularly monitor. datafile —> memory — > output device (screen) Flow of data

Open Modes Mode Flag Description ios ::app Append mode. All output to that file to be appended to the end. ios ::ate Open a file for output and move the read/write control to the end of the file. ios ::in Open a file for reading. ios ::out Open a file for writing. ios :: trunc If the file already exists, its contents will be truncated before opening the file.

Open files fstream afile ; afile.open ("file.dat", ios ::out | ios ::in ); ofstream outfile ; outfile.open ("file.dat", ios ::out | ios :: trunc );

Closing file FileName.Close ();

Writing in a file While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object

Reading from a file You read information from a file into your program using the stream extraction operator (<<) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.

File has two associated pointers called input pointer (or get pointer) and output pointer (or put pointer). Each time an input or output operation takes place, the pointer moves automatically. There are two pointers. seekg ( ) It moves get pointer to a specified location. seekp ( ) It moves the put pointer to a specified location File pointers

Ios :: beg ios :: cur ios ::end ios :: beg means start of the file ios :: cur means current position of the pointer ios :: end means end of the file File pointers file

A pointer is a variable that represents the location (rather than the value ) of a data item such as a variable or an array element Pointer

Pointer variables declare with * sign. And pointer variables access with & sign. How to declare pointer variables?

Int a = 10; Int b = &a; In above example variable “a” is integer type variable and variable “b” is also integer variable. Variable b store the reference of variable “a” and when we use the variable “b” it return the value of variable “a”. Pointer example

Operator overloading is use to overload any predefined operators. Create class and with operator method and write new code for any operator. Operator overloading

class class_name { public : void operator sign of operator () { new logic for operator }; }; Main method { object and apply operator } Syntax of operator overloading

Class demo { Public: Void operator ++ () { Cout <<“ ++ operator overloaded”; } }; Void main() { demo d; d++ } Example

Class name is demo. Publicly overload ++ operator with simple message. d is object of demo class. whenever you apply ++ operator then compiler not increment any value in object, But print simple message on screen. Explanation of previous program

Prepared by Kalpesh chauhan & Kaushik bhalodiya

T h a n k y o u
Tags