68
ACA Summer School 2014 Advanced C++ Pankaj Prateek ACA, CSE, IIT Kanpur June 23, 2014

Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

ACA Summer School 2014Advanced C++

Pankaj Prateek

ACA, CSE, IIT Kanpur

June 23, 2014

Page 2: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

I Course website:http://www.cse.iitk.ac.in/users/aca/sumschool2014.html

I EvaluationI End Sem - 50%I Assignments / In-class quiz - 50%

I Timings:I M-F 1430 - 1600 hrs

Page 3: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Prerequisites

I A good command over any programming language preferablyC/C++

I PointersI Structures

Page 4: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I How do you store the details of a person?

char strName [20];int nBirthYear;int nBirthMonth;int nBirthDay;int nHeight;

I Information is not grouped in any way.I To pass your information to a function, you would have to

pass each variable independently.I If wanted to store information about many people, would have

to declare arrays.

Page 5: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I How do you store the details of a person?

char strName [20];int nBirthYear;int nBirthMonth;int nBirthDay;int nHeight;

I Information is not grouped in any way.I To pass your information to a function, you would have to

pass each variable independently.I If wanted to store information about many people, would have

to declare arrays.

Page 6: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I How do you store the details of a person?

char strName [20];int nBirthYear;int nBirthMonth;int nBirthDay;int nHeight;

I Information is not grouped in any way.I To pass your information to a function, you would have to

pass each variable independently.I If wanted to store information about many people, would have

to declare arrays.

Page 7: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I How do you store the details of a person?

char strName [20];int nBirthYear;int nBirthMonth;int nBirthDay;int nHeight;

I Information is not grouped in any way.I To pass your information to a function, you would have to

pass each variable independently.I If wanted to store information about many people, would have

to declare arrays.

Page 8: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I How do you store the details of a person?

char strName [20];int nBirthYear;int nBirthMonth;int nBirthDay;int nHeight;

I Information is not grouped in any way.I To pass your information to a function, you would have to

pass each variable independently.I If wanted to store information about many people, would have

to declare arrays.

Page 9: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I C++ allows to create own user-defined data types to aggregatedifferent variables : structs

I Structure declaration

struct Person{char strName [20];int nBirthYear;int nBirthMonth;int nBirthDay;int nHeight;

}; //DO NOT forget a semicolon

I Now person structure can be used as a built-in variable.

Page 10: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I C++ allows to create own user-defined data types to aggregatedifferent variables : structs

I Structure declaration

struct Person{char strName [20];int nBirthYear;int nBirthMonth;int nBirthDay;int nHeight;

}; //DO NOT forget a semicolon

I Now person structure can be used as a built-in variable.

Page 11: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I C++ allows to create own user-defined data types to aggregatedifferent variables : structs

I Structure declaration

struct Person{char strName [20];int nBirthYear;int nBirthMonth;int nBirthDay;int nHeight;

}; //DO NOT forget a semicolon

I Now person structure can be used as a built-in variable.

Page 12: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I Usage

struct Person p1;struct Person p2;

I Accessing Members:

p1.name = ‘‘pankaj ’’;p1.nBirthDay = 20;p2.name = ‘‘Rahul’’;

Page 13: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I Usage

struct Person p1;struct Person p2;

I Accessing Members:

p1.name = ‘‘pankaj ’’;p1.nBirthDay = 20;p2.name = ‘‘Rahul’’;

Page 14: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I No memory is allocated in structure declarationI Size of a structure is the sum of sizes of it elementsI Can pass entire structures to functions

void PrintInfo(struct Person p) {cout << ‘‘Name: ’’ << p.name <<endl;cout << ‘‘bDay: ’’ << p.nBirthDay <<endl;

}int main() {

struct Person p1;p1.name = ‘‘Pankaj ’’;p1.nBirthDay = 20;PrintInfo(p1);

}

Page 15: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I No memory is allocated in structure declarationI Size of a structure is the sum of sizes of it elementsI Can pass entire structures to functions

void PrintInfo(struct Person p) {cout << ‘‘Name: ’’ << p.name <<endl;cout << ‘‘bDay: ’’ << p.nBirthDay <<endl;

}int main() {

struct Person p1;p1.name = ‘‘Pankaj ’’;p1.nBirthDay = 20;PrintInfo(p1);

}

Page 16: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I No memory is allocated in structure declarationI Size of a structure is the sum of sizes of it elementsI Can pass entire structures to functions

void PrintInfo(struct Person p) {cout << ‘‘Name: ’’ << p.name <<endl;cout << ‘‘bDay: ’’ << p.nBirthDay <<endl;

}int main() {

struct Person p1;p1.name = ‘‘Pankaj ’’;p1.nBirthDay = 20;PrintInfo(p1);

}

Page 17: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I Always have to use the word “struct”I No explicit connection between members of a structure and

the functions manipulating themI Cannot be treated as built-in types (c1 + c2 is not valid for

instances of “struct complex”)I Data hiding is not permitted (Why do we need this?)I All members of a structure are by defaut “public” (will be

discussed later)

Page 18: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I Always have to use the word “struct”I No explicit connection between members of a structure and

the functions manipulating themI Cannot be treated as built-in types (c1 + c2 is not valid for

instances of “struct complex”)I Data hiding is not permitted (Why do we need this?)I All members of a structure are by defaut “public” (will be

discussed later)

Page 19: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I Always have to use the word “struct”I No explicit connection between members of a structure and

the functions manipulating themI Cannot be treated as built-in types (c1 + c2 is not valid for

instances of “struct complex”)I Data hiding is not permitted (Why do we need this?)I All members of a structure are by defaut “public” (will be

discussed later)

Page 20: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I Always have to use the word “struct”I No explicit connection between members of a structure and

the functions manipulating themI Cannot be treated as built-in types (c1 + c2 is not valid for

instances of “struct complex”)I Data hiding is not permitted (Why do we need this?)I All members of a structure are by defaut “public” (will be

discussed later)

Page 21: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Structures

I Always have to use the word “struct”I No explicit connection between members of a structure and

the functions manipulating themI Cannot be treated as built-in types (c1 + c2 is not valid for

instances of “struct complex”)I Data hiding is not permitted (Why do we need this?)I All members of a structure are by defaut “public” (will be

discussed later)

Page 22: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Classes

I Extension of structuresI Class Definition

class class_name {private:

variable declarations;function declarations;

public:variable declarations;function declarations;

}; //DO NOT forget the semicolon

Page 23: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Classes

I Extension of structuresI Class Definition

class class_name {private:

variable declarations;function declarations;

public:variable declarations;function declarations;

}; //DO NOT forget the semicolon

Page 24: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Example

I Example

class item {private:

int number; // variable declarationsfloat cost; // private by default!

public:// function declarations through prototypesvoid getData(int a, float b);void putData(void);

}

Page 25: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class

No memory allocated for a class definiton. It is only a “template”,like a definition of a structure

Page 26: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Public and Private

I Private members are not directly accessible outside the class.They are “hidden” from the outside world. The only way toaccess them is by using public functions (if defined) whichmanipulate them.

I Public members can be accessed using the dot operator(member selection operator). Eg. student.name,item.getData()

Page 27: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Public and Private

I Private members are not directly accessible outside the class.They are “hidden” from the outside world. The only way toaccess them is by using public functions (if defined) whichmanipulate them.

I Public members can be accessed using the dot operator(member selection operator). Eg. student.name,item.getData()

Page 28: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Objects

I Class, like a structure definition, is a user-defined data typewithout any concrete existance.

I A concrete instance of a class is an object.I Memory is allocated for every object that is created.I An independent new set of variables is created for each object.I Public members can be accessed by using the dot operator on

the object while private members cannot be accessed directly.

Page 29: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Objects

I Class, like a structure definition, is a user-defined data typewithout any concrete existance.

I A concrete instance of a class is an object.I Memory is allocated for every object that is created.I An independent new set of variables is created for each object.I Public members can be accessed by using the dot operator on

the object while private members cannot be accessed directly.

Page 30: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Objects

I Class, like a structure definition, is a user-defined data typewithout any concrete existance.

I A concrete instance of a class is an object.I Memory is allocated for every object that is created.I An independent new set of variables is created for each object.I Public members can be accessed by using the dot operator on

the object while private members cannot be accessed directly.

Page 31: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Objects

I Class, like a structure definition, is a user-defined data typewithout any concrete existance.

I A concrete instance of a class is an object.I Memory is allocated for every object that is created.I An independent new set of variables is created for each object.I Public members can be accessed by using the dot operator on

the object while private members cannot be accessed directly.

Page 32: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Objects

I Class, like a structure definition, is a user-defined data typewithout any concrete existance.

I A concrete instance of a class is an object.I Memory is allocated for every object that is created.I An independent new set of variables is created for each object.I Public members can be accessed by using the dot operator on

the object while private members cannot be accessed directly.

Page 33: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Objects

Object Creation

I Just like variable declarationI Memory is allocated for every object that is createdI Example:

item a;item b,c,d;

Page 34: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Objects

Object Creation

I Just like variable declarationI Memory is allocated for every object that is createdI Example:

item a;item b,c,d;

Page 35: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Objects

Object Creation

I Just like variable declarationI Memory is allocated for every object that is createdI Example:

item a;item b,c,d;

Page 36: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Objects

Object Creation

I Just like variable declarationI Memory is allocated for every object that is createdI Example:

item a;item b,c,d;

Page 37: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Function definitions

Outside the class

class Employee {int empno , salary;

public:void set(int roll , int sal);

};

void employee ::set(int roll , int sal) {empno = roll;salary = sal;

}

Page 38: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Function definitions

Outside the class

class Employee {int empno , salary;

public:void set(int roll , int sal);

};

void employee ::set(int roll , int sal) {empno = roll;salary = sal;

}

Page 39: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Function definitions

Inside the class

class Employee {int empno , salary;

public:void set(int roll , int sal) {

empno = roll;salary = sal;

}};

Page 40: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Function definitions

Inside the class

class Employee {int empno , salary;

public:void set(int roll , int sal) {

empno = roll;salary = sal;

}};

Page 41: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Important Properties

I Invoking other member functions from inside a memberfunction does not require explicit use of the object

I Array size, if used inside classes, need to be determined atcompile time (for dynamic arrays, with size to be determinedat run time, “new” operator is used inside a constructor, willbe discussed later)

I Arrays of objects are allowed (stored contiguously). Objectscan be used as members of some other class in nested fashion.

I Objects, just like built-in types, can be return type offunctions.

I Private functions and variables can only be accessed fromwithin the class.

Page 42: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Important Properties

I Invoking other member functions from inside a memberfunction does not require explicit use of the object

I Array size, if used inside classes, need to be determined atcompile time (for dynamic arrays, with size to be determinedat run time, “new” operator is used inside a constructor, willbe discussed later)

I Arrays of objects are allowed (stored contiguously). Objectscan be used as members of some other class in nested fashion.

I Objects, just like built-in types, can be return type offunctions.

I Private functions and variables can only be accessed fromwithin the class.

Page 43: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Important Properties

I Invoking other member functions from inside a memberfunction does not require explicit use of the object

I Array size, if used inside classes, need to be determined atcompile time (for dynamic arrays, with size to be determinedat run time, “new” operator is used inside a constructor, willbe discussed later)

I Arrays of objects are allowed (stored contiguously). Objectscan be used as members of some other class in nested fashion.

I Objects, just like built-in types, can be return type offunctions.

I Private functions and variables can only be accessed fromwithin the class.

Page 44: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Important Properties

I Invoking other member functions from inside a memberfunction does not require explicit use of the object

I Array size, if used inside classes, need to be determined atcompile time (for dynamic arrays, with size to be determinedat run time, “new” operator is used inside a constructor, willbe discussed later)

I Arrays of objects are allowed (stored contiguously). Objectscan be used as members of some other class in nested fashion.

I Objects, just like built-in types, can be return type offunctions.

I Private functions and variables can only be accessed fromwithin the class.

Page 45: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Important Properties

I Invoking other member functions from inside a memberfunction does not require explicit use of the object

I Array size, if used inside classes, need to be determined atcompile time (for dynamic arrays, with size to be determinedat run time, “new” operator is used inside a constructor, willbe discussed later)

I Arrays of objects are allowed (stored contiguously). Objectscan be used as members of some other class in nested fashion.

I Objects, just like built-in types, can be return type offunctions.

I Private functions and variables can only be accessed fromwithin the class.

Page 46: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Classes

ProblemI Consider a class “car” which has the carNo, carModel, carMake

fields and relevant functions to modify them.I You have to count the number of objects of the class created.

How do you do it?

Page 47: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Classes

ProblemI Consider a class “car” which has the carNo, carModel, carMake

fields and relevant functions to modify them.I You have to count the number of objects of the class created.

How do you do it?

Page 48: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Classes

ProblemI Consider a class “car” which has the carNo, carModel, carMake

fields and relevant functions to modify them.I You have to count the number of objects of the class created.

How do you do it?

Page 49: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Static Members

class item {static int count;// rest of class definition

};int item::count;

Page 50: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Static Members

Properties

I Every static member needs to be defined outside the class aswell.

I Only one copy of the static variable is shared among all objectsof the class.

I Visible only within the class but exists for the lifetime of theprogram.

I No object instantiation is required to access static members.

Page 51: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Static Members

Properties

I Every static member needs to be defined outside the class aswell.

I Only one copy of the static variable is shared among all objectsof the class.

I Visible only within the class but exists for the lifetime of theprogram.

I No object instantiation is required to access static members.

Page 52: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Static Members

Properties

I Every static member needs to be defined outside the class aswell.

I Only one copy of the static variable is shared among all objectsof the class.

I Visible only within the class but exists for the lifetime of theprogram.

I No object instantiation is required to access static members.

Page 53: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Static Members

Properties

I Every static member needs to be defined outside the class aswell.

I Only one copy of the static variable is shared among all objectsof the class.

I Visible only within the class but exists for the lifetime of theprogram.

I No object instantiation is required to access static members.

Page 54: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Static Members

Properties

I Every static member needs to be defined outside the class aswell.

I Only one copy of the static variable is shared among all objectsof the class.

I Visible only within the class but exists for the lifetime of theprogram.

I No object instantiation is required to access static members.

Page 55: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Memory Allocation of Objects

I For member functions and static variables, memory is allocatedwhen the class is defined, all objects of the class share thesame function code and static variables (every class does notget its own copy). “this” pointer is implicitly passed so thatthe function code is executed on the correct class instance.

I For variables, memory is allocated when the object is defined.Each instance gets its own set of variables.

Page 56: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Memory Allocation of Objects

I For member functions and static variables, memory is allocatedwhen the class is defined, all objects of the class share thesame function code and static variables (every class does notget its own copy). “this” pointer is implicitly passed so thatthe function code is executed on the correct class instance.

I For variables, memory is allocated when the object is defined.Each instance gets its own set of variables.

Page 57: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Shared functions among multiple classesI Properties:

I Can access private members of the classI Often used in operator overloadingI Not in the scope of the class. Cannot be called using an object

of the class.I Can be invoked like a normal functionI Cannot access members of a class directly without an object of

the class

Page 58: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Shared functions among multiple classesI Properties:

I Can access private members of the classI Often used in operator overloadingI Not in the scope of the class. Cannot be called using an object

of the class.I Can be invoked like a normal functionI Cannot access members of a class directly without an object of

the class

Page 59: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Shared functions among multiple classesI Properties:

I Can access private members of the classI Often used in operator overloadingI Not in the scope of the class. Cannot be called using an object

of the class.I Can be invoked like a normal functionI Cannot access members of a class directly without an object of

the class

Page 60: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Shared functions among multiple classesI Properties:

I Can access private members of the classI Often used in operator overloadingI Not in the scope of the class. Cannot be called using an object

of the class.I Can be invoked like a normal functionI Cannot access members of a class directly without an object of

the class

Page 61: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Shared functions among multiple classesI Properties:

I Can access private members of the classI Often used in operator overloadingI Not in the scope of the class. Cannot be called using an object

of the class.I Can be invoked like a normal functionI Cannot access members of a class directly without an object of

the class

Page 62: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Shared functions among multiple classesI Properties:

I Can access private members of the classI Often used in operator overloadingI Not in the scope of the class. Cannot be called using an object

of the class.I Can be invoked like a normal functionI Cannot access members of a class directly without an object of

the class

Page 63: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Shared functions among multiple classesI Properties:

I Can access private members of the classI Often used in operator overloadingI Not in the scope of the class. Cannot be called using an object

of the class.I Can be invoked like a normal functionI Cannot access members of a class directly without an object of

the class

Page 64: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Properties: (cont. . . )I Can be declared as either private or public without changing

the meaningI Objects can be passed to the function by value or by referenceI A class can be defined to be the friend of another class. In

that case, all member functions of one class are friends of theother class

Page 65: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Properties: (cont. . . )I Can be declared as either private or public without changing

the meaningI Objects can be passed to the function by value or by referenceI A class can be defined to be the friend of another class. In

that case, all member functions of one class are friends of theother class

Page 66: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Properties: (cont. . . )I Can be declared as either private or public without changing

the meaningI Objects can be passed to the function by value or by referenceI A class can be defined to be the friend of another class. In

that case, all member functions of one class are friends of theother class

Page 67: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

Class: Friend

I Properties: (cont. . . )I Can be declared as either private or public without changing

the meaningI Objects can be passed to the function by value or by referenceI A class can be defined to be the friend of another class. In

that case, all member functions of one class are friends of theother class

Page 68: Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK

References

I The C++ Programming Language- Bjarne Stroustrup

I Object Oriented Programming with C++- E Balaguruswamy

I http://www.cplusplus.com/referenceI http://www.learncpp.com/I http://www.java2s.com/Code/Cpp/CatalogCpp.htm