c++ theory
TRANSCRIPT
![Page 1: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/1.jpg)
C++ Theory
![Page 2: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/2.jpg)
Object Oriented
Programming
language
![Page 3: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/3.jpg)
C++ is an extension to C Programming language. It was developed at AT&T BellLaboratories in the early 1980s by Bjarne Stroustrup. It is a deviation from traditionalprocedural 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++
![Page 4: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/4.jpg)
Inheritance Polymorphism Encapsulation
Oop Concepts. . .
![Page 5: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/5.jpg)
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
![Page 6: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/6.jpg)
inheritance
vehicle
2 wheeler 4 wheeler
honda Yamaha tata mahindra
![Page 7: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/7.jpg)
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
![Page 8: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/8.jpg)
EncapsulationPrivate data
Public data
Calling object
![Page 9: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/9.jpg)
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
![Page 10: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/10.jpg)
pollymurphism
What is your
name???
amit
sanjay
prakash
![Page 11: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/11.jpg)
Pre processing directive.
Header files Iostream.h Input output stream header file.
Containing Cin and cout menthod
#include
![Page 12: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/12.jpg)
C++ is totally case sensitive language.
Important note
![Page 13: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/13.jpg)
cout<<“ welcome ”;
◦ cout = method◦ << = operator◦ “welcome “ = content to write◦ ; = statement termination
Writing on screen
![Page 14: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/14.jpg)
Int k; cin>>k;
◦ cin = method◦ >> = operator◦ k = variable name to store data◦ ; = statement termination
Reading the data from keyboard
![Page 15: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/15.jpg)
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
![Page 16: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/16.jpg)
#include<iostream.h> #include<conio.h> #include<stdio.h>
Void main() {
◦ Clrscr();◦ Cout<<“my first program in c++”;◦ Getch();
}
My first program
![Page 17: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/17.jpg)
Registered and reserved words of c++ are known as a key word.
Not use to create variables.
Key word
![Page 18: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/18.jpg)
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++
![Page 19: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/19.jpg)
Brackets [ ] opening and closing brackets indicatesingle 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
![Page 20: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/20.jpg)
Char Integer Float Double Boolean
Data types
![Page 21: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/21.jpg)
If condition If else condition If else if condition Switch case
Break statement Continue statement
Control statement
![Page 22: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/22.jpg)
If(condition) {
◦ Executable part }
If condition
![Page 23: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/23.jpg)
If(condition){
execute when condition ins true}else{
execute when condition is false}
If else
![Page 24: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/24.jpg)
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
![Page 25: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/25.jpg)
Switch(parameter) {
◦ Case (condition):◦ {◦ executable part;◦ Break;
}◦ Case (condition):◦ {◦ executable part;◦ Break;
} }
Switch case
![Page 26: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/26.jpg)
Looping structure
Entry control loop
• For loop• While loop
Exit control loop
• Do while loop
![Page 27: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/27.jpg)
While(condition) {
◦ Executable part◦ Increment / decrement;
}
While loop
![Page 28: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/28.jpg)
Do {
◦ Executable part;◦ Increment / decrement
}while(condition);
Do while loop
Note : do while loop compulsory execute once
![Page 29: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/29.jpg)
For(initialization;condition;increment/decrement){◦ Executable part
}
For loop
![Page 30: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/30.jpg)
Arithmetic Logical Conditional Relational Increment / decrement Special Assignment Urinary / short hand
Operator
![Page 31: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/31.jpg)
+ ( summation ) - ( subtraction ) / ( division ) * ( multiplication ) % ( modulation )
Arithmetic
![Page 32: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/32.jpg)
|| ( or sign ) && ( and sign ) ! ( not sign )
Logical
![Page 33: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/33.jpg)
Condition 1 Condition 2 ResultTrue False FalseFalse True FalseFalse False FalseTrue True True
And (&&) operator( flow chart)
![Page 34: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/34.jpg)
Condition 1 Condition 2 ResultTrue False TrueFalse True trueFalse False FalseTrue True True
Or (||) operator ( flow chart)
![Page 35: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/35.jpg)
Condition 1 condition 2 ResultTrue False FalseFalse True FalseFalse False TrueTrue True False
Not ( ! ) operator flow chart
![Page 36: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/36.jpg)
> ( greater than) < ( less than) >= ( greater than equal) <= ( less than equal ) == ( equals to ) != ( not equals )
Relational
![Page 37: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/37.jpg)
(condition? true: false)
conditional
Special operator , ( comma operator )
Ex. int a, b, c;
![Page 38: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/38.jpg)
++ --
Pre increment / decrement◦ ++ Variable name◦ -- variable name
Post increment / decrement◦ variable name ++◦ variable name --
Increment / decrement
![Page 39: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/39.jpg)
Variable name += Variable name +- Variable name +/ Variable name +* Variable name +%
Compound assignment
![Page 40: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/40.jpg)
Ex cout<<“string 1” << “string 2” ;
Concatination
![Page 41: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/41.jpg)
All the statement of c ++ are terminated by ◦ ;◦ (semi colon )
Must remember
![Page 42: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/42.jpg)
String functions Math functions Character functions
Inbuilt functions
![Page 43: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/43.jpg)
Header file◦ String.h
Functionsstrlenstrrevstrcpystrcmpstruprstrlwr
String functions
![Page 44: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/44.jpg)
Header file◦ math.h
MinMaxRoundSqrtAbsFloorCeil
Math functions
![Page 45: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/45.jpg)
Header file◦ Ctype.h
◦ Getch◦ Getche◦ Isdigit◦ Isalpha◦ Isalnum◦ Isspace
Character functions
![Page 46: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/46.jpg)
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
![Page 47: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/47.jpg)
Abc()
Void Abc() {
◦ Executable part }
No parameter no return value
![Page 48: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/48.jpg)
Int I = abc();
Int abc(){
return 5;}
note : value of I = 5
No parameter with return
![Page 49: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/49.jpg)
Abc(5,10);
Void Abc(a,b) {
◦ Cout<<5+10; }
With parameter no return
![Page 50: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/50.jpg)
Int I = abc(5,10);
Int abc(a,b); {
◦ Return a + b; }
Value of I = 15.
With parameter with return value
![Page 51: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/51.jpg)
Single dimention Multi dimention
◦ Collection of same data type.
◦ Int a[0];
Array
![Page 52: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/52.jpg)
Index of array start with 0 (zero)
Array cont…..
Index
10A[0]
A[1]
A[2]
A[3]
20
30
40
Int A[4]
value
![Page 53: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/53.jpg)
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
![Page 54: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/54.jpg)
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
![Page 55: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/55.jpg)
Collection of different data type
User define data type
Access by . (Dot)
Define with struct keyword
structure
![Page 56: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/56.jpg)
struct structure name {
Variable data type variable name Variable data type variable name Variable data type variable name
Up to n variables. . .
};
syntax
![Page 57: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/57.jpg)
struct std {
◦ int rn;◦ float per;
}; Void main() {
◦ std s;◦ S.rn = 915;
}
example
![Page 58: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/58.jpg)
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
![Page 59: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/59.jpg)
Return type class name :: method name( list of parameters)
Syntax
![Page 60: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/60.jpg)
class abc {
◦ Public:◦ Void msg();
};◦ void abc :: msg()◦ {
Executable part◦ }
example
![Page 61: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/61.jpg)
Class : Collection of methods and variables. Collection of private and public data.
Object : Instants that Use to access functionality of class.
Class and object
![Page 62: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/62.jpg)
class class_name {
Public : Methods Variables
};
Class
![Page 63: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/63.jpg)
Return type method name ( list of parameters)
{◦ Executable part◦ [ return data ]
};
Method
![Page 64: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/64.jpg)
Class name object name;
Ex.abc a;
Class name abcObject name a
Creating object
![Page 65: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/65.jpg)
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
![Page 66: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/66.jpg)
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.
![Page 67: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/67.jpg)
class abc {
◦ public:◦ abc()◦ {
Executable part◦ }
};
Constructor example
![Page 68: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/68.jpg)
Use to delete object of any class
Call by ~ sign.
Destructor never return any kind of values.
Destructor
![Page 69: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/69.jpg)
class abc {
public :◦ ~ abc()◦ {
◦ } }
Destructor example
![Page 70: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/70.jpg)
class abc {
public : void msg() {
cout<<“welcome ”; };
}; void main {
◦ abc a;◦ a.msg();
} o/p = welcome
Accessing method through object
![Page 71: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/71.jpg)
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
![Page 72: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/72.jpg)
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++
![Page 73: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/73.jpg)
Base class Derived classAccess public private
protectedspecifierpublic public private
protectedprivate Not Not Not
inherited inheritedinherited
protected protected protected protected
Mode of inheritance
![Page 74: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/74.jpg)
Ex. Functionality of class A use in class B
Single level inheritance
A
B
![Page 75: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/75.jpg)
Multi level inheritance
a
b
c
d
![Page 76: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/76.jpg)
Multiple inheritance
c
a b
![Page 77: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/77.jpg)
Hierarchical inheritance
a
b c d
![Page 78: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/78.jpg)
Combination of hierarchical and multiple
Hybrid inheritance
a
d
b b
![Page 79: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/79.jpg)
Remember Available types of inheritance is
◦ Single level inheritance◦ Multi level inheritance◦ Multiple inheritance◦ Hierarchical inheritance◦ Hybrid inheritance
Example and syntaxes of inheritance
![Page 80: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/80.jpg)
Ex. Functionality of class A use in class B
Single level inheritance
A
B
![Page 81: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/81.jpg)
Base Class name : mode of inheritance super class name
Different Modes◦ Public◦ Private◦ Protected
Syntax of single level inheritance
![Page 82: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/82.jpg)
class abc {
◦ Methods and variable };
class xyz : public abc {
◦ Methods and variable };
Single level inheritance example
![Page 83: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/83.jpg)
Multi level inheritance
a
b
c
d
![Page 84: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/84.jpg)
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
![Page 85: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/85.jpg)
Class demo{}
Class demo1: public demo{}
Class demo2 : public demo1{}
example
![Page 86: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/86.jpg)
Multiple inheritance
c
a b
![Page 87: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/87.jpg)
Class class name{}class class name{}class class name : access specification class name ( parent 1 ), (parent 2 )
Syntax of multiple inheritance
![Page 88: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/88.jpg)
Class demo{}
Class demo1{}
Class final : public demo, public demo1{}
Example
![Page 89: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/89.jpg)
Hierarchical inheritance
a
b c d
![Page 90: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/90.jpg)
Class class name{}
Class calss name : access specification class name{}
Class class name : access specification class name{}
Hierarchical inheritance syntax
![Page 91: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/91.jpg)
Class demo{}
Class demo1 : public demo{}
Class demo2 : public demo{}
Example
![Page 92: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/92.jpg)
Combination of hierarchical
Hybrid inheritance
a
d
b b
![Page 93: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/93.jpg)
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
![Page 94: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/94.jpg)
Class demo {} Class demo1 : public demo {} Class demo2 : public demo {} Class final : public demo1, public demo2 {}
examle
![Page 95: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/95.jpg)
A file is a collection of logically related records. A program usually requires twotypes of data communication.
File handling
![Page 96: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/96.jpg)
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.
![Page 97: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/97.jpg)
(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
![Page 98: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/98.jpg)
Open ModesMode 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.
![Page 99: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/99.jpg)
Open filesfstream afile; afile.open("file.dat", ios::out | ios::in );
ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc );
![Page 100: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/100.jpg)
Closing file
FileName.Close();
![Page 101: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/101.jpg)
Writing in a fileWhile 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
![Page 102: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/102.jpg)
Reading from a fileYou 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.
![Page 103: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/103.jpg)
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
![Page 104: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/104.jpg)
Ios:: beg ios:: curios::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
![Page 105: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/105.jpg)
A pointer is a variable that represents the location (rather than the value ) of a dataitem such as a variable or an array element
Pointer
![Page 106: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/106.jpg)
Pointer variables declare with * sign.
And pointer variables access with & sign.
How to declare pointer variables?
![Page 107: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/107.jpg)
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
![Page 108: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/108.jpg)
Operator overloading is use to overload any predefined operators.
Create class and with operator method and write new code for any operator.
Operator overloading
![Page 109: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/109.jpg)
class class_name{public :void operator sign of operator () {new logic for operator };};
Main method {
object and apply operator}
Syntax of operator overloading
![Page 110: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/110.jpg)
Class demo{
◦ Public:◦ Void operator ++ ()◦ {
Cout<<“ ++ operator overloaded”;◦ }
};Void main(){
demo d;d++
}
Example
![Page 111: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/111.jpg)
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
![Page 112: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/112.jpg)
Prepared by
Kalpesh chauhan&
Kaushik bhalodiya
![Page 113: C++ theory](https://reader036.vdocuments.net/reader036/viewer/2022070602/587d278b1a28ab1c2f8b6c4b/html5/thumbnails/113.jpg)
Thank you