SlideShare uma empresa Scribd logo
1 de 68
Baixar para ler offline
ACA Summer School 2014
Advanced C++
Pankaj Prateek
ACA, CSE, IIT Kanpur
June 23, 2014
Course website:
http:
//www.cse.iitk.ac.in/users/aca/sumschool2014.html
Evaluation
End Sem - 50%
Assignments / In-class quiz - 50%
Timings:
M-F 1430 - 1600 hrs
Prerequisites
A good command over any programming language preferably
C/C++
Pointers
Structures
Structures
How do you store the details of a person?
char strName [20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight;
Information is not grouped in any way.
To pass your information to a function, you would have to
pass each variable independently.
If wanted to store information about many people, would have
to declare arrays.
Structures
How do you store the details of a person?
char strName [20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight;
Information is not grouped in any way.
To pass your information to a function, you would have to
pass each variable independently.
If wanted to store information about many people, would have
to declare arrays.
Structures
How do you store the details of a person?
char strName [20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight;
Information is not grouped in any way.
To pass your information to a function, you would have to
pass each variable independently.
If wanted to store information about many people, would have
to declare arrays.
Structures
How do you store the details of a person?
char strName [20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight;
Information is not grouped in any way.
To pass your information to a function, you would have to
pass each variable independently.
If wanted to store information about many people, would have
to declare arrays.
Structures
How do you store the details of a person?
char strName [20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight;
Information is not grouped in any way.
To pass your information to a function, you would have to
pass each variable independently.
If wanted to store information about many people, would have
to declare arrays.
Structures
C++ allows to create own user-defined data types to aggregate
different variables : structs
Structure declaration
struct Person{
char strName [20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight;
}; //DO NOT forget a semicolon
Now person structure can be used as a built-in variable.
Structures
C++ allows to create own user-defined data types to aggregate
different variables : structs
Structure declaration
struct Person{
char strName [20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight;
}; //DO NOT forget a semicolon
Now person structure can be used as a built-in variable.
Structures
C++ allows to create own user-defined data types to aggregate
different variables : structs
Structure declaration
struct Person{
char strName [20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight;
}; //DO NOT forget a semicolon
Now person structure can be used as a built-in variable.
Structures
Usage
struct Person p1;
struct Person p2;
Accessing Members:
p1.name = ‘‘pankaj ’’;
p1.nBirthDay = 20;
p2.name = ‘‘Rahul ’’;
Structures
Usage
struct Person p1;
struct Person p2;
Accessing Members:
p1.name = ‘‘pankaj ’’;
p1.nBirthDay = 20;
p2.name = ‘‘Rahul ’’;
Structures
No memory is allocated in structure declaration
Size of a structure is the sum of sizes of it elements
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);
}
Structures
No memory is allocated in structure declaration
Size of a structure is the sum of sizes of it elements
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);
}
Structures
No memory is allocated in structure declaration
Size of a structure is the sum of sizes of it elements
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);
}
Structures
Always have to use the word “struct”
No explicit connection between members of a structure and
the functions manipulating them
Cannot be treated as built-in types (c1 + c2 is not valid for
instances of “struct complex”)
Data hiding is not permitted (Why do we need this?)
All members of a structure are by defaut “public” (will be
discussed later)
Structures
Always have to use the word “struct”
No explicit connection between members of a structure and
the functions manipulating them
Cannot be treated as built-in types (c1 + c2 is not valid for
instances of “struct complex”)
Data hiding is not permitted (Why do we need this?)
All members of a structure are by defaut “public” (will be
discussed later)
Structures
Always have to use the word “struct”
No explicit connection between members of a structure and
the functions manipulating them
Cannot be treated as built-in types (c1 + c2 is not valid for
instances of “struct complex”)
Data hiding is not permitted (Why do we need this?)
All members of a structure are by defaut “public” (will be
discussed later)
Structures
Always have to use the word “struct”
No explicit connection between members of a structure and
the functions manipulating them
Cannot be treated as built-in types (c1 + c2 is not valid for
instances of “struct complex”)
Data hiding is not permitted (Why do we need this?)
All members of a structure are by defaut “public” (will be
discussed later)
Structures
Always have to use the word “struct”
No explicit connection between members of a structure and
the functions manipulating them
Cannot be treated as built-in types (c1 + c2 is not valid for
instances of “struct complex”)
Data hiding is not permitted (Why do we need this?)
All members of a structure are by defaut “public” (will be
discussed later)
Classes
Extension of structures
Class Definition
class class_name {
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
}; //DO NOT forget the semicolon
Classes
Extension of structures
Class Definition
class class_name {
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
}; //DO NOT forget the semicolon
Class: Example
Example
class item {
private:
int number; // variable declarations
float cost; // private by default!
public:
// function declarations through prototypes
void getData(int a, float b);
void putData(void );
}
Class
No memory allocated for a class definiton. It is only a “template”,
like a definition of a structure
Class: Public and Private
Private members are not directly accessible outside the class.
They are “hidden” from the outside world. The only way to
access them is by using public functions (if defined) which
manipulate them.
Public members can be accessed using the dot operator
(member selection operator). Eg. student.name,
item.getData()
Class: Public and Private
Private members are not directly accessible outside the class.
They are “hidden” from the outside world. The only way to
access them is by using public functions (if defined) which
manipulate them.
Public members can be accessed using the dot operator
(member selection operator). Eg. student.name,
item.getData()
Class: Objects
Class, like a structure definition, is a user-defined data type
without any concrete existance.
A concrete instance of a class is an object.
Memory is allocated for every object that is created.
An independent new set of variables is created for each object.
Public members can be accessed by using the dot operator on
the object while private members cannot be accessed directly.
Class: Objects
Class, like a structure definition, is a user-defined data type
without any concrete existance.
A concrete instance of a class is an object.
Memory is allocated for every object that is created.
An independent new set of variables is created for each object.
Public members can be accessed by using the dot operator on
the object while private members cannot be accessed directly.
Class: Objects
Class, like a structure definition, is a user-defined data type
without any concrete existance.
A concrete instance of a class is an object.
Memory is allocated for every object that is created.
An independent new set of variables is created for each object.
Public members can be accessed by using the dot operator on
the object while private members cannot be accessed directly.
Class: Objects
Class, like a structure definition, is a user-defined data type
without any concrete existance.
A concrete instance of a class is an object.
Memory is allocated for every object that is created.
An independent new set of variables is created for each object.
Public members can be accessed by using the dot operator on
the object while private members cannot be accessed directly.
Class: Objects
Class, like a structure definition, is a user-defined data type
without any concrete existance.
A concrete instance of a class is an object.
Memory is allocated for every object that is created.
An independent new set of variables is created for each object.
Public members can be accessed by using the dot operator on
the object while private members cannot be accessed directly.
Class: Objects
Object Creation
Just like variable declaration
Memory is allocated for every object that is created
Example:
item a;
item b,c,d;
Class: Objects
Object Creation
Just like variable declaration
Memory is allocated for every object that is created
Example:
item a;
item b,c,d;
Class: Objects
Object Creation
Just like variable declaration
Memory is allocated for every object that is created
Example:
item a;
item b,c,d;
Class: Objects
Object Creation
Just like variable declaration
Memory is allocated for every object that is created
Example:
item a;
item b,c,d;
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;
}
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;
}
Class: Function definitions
Inside the class
class Employee {
int empno , salary;
public:
void set(int roll , int sal) {
empno = roll;
salary = sal;
}
};
Class: Function definitions
Inside the class
class Employee {
int empno , salary;
public:
void set(int roll , int sal) {
empno = roll;
salary = sal;
}
};
Important Properties
Invoking other member functions from inside a member
function does not require explicit use of the object
Array size, if used inside classes, need to be determined at
compile time (for dynamic arrays, with size to be determined
at run time, “new” operator is used inside a constructor, will
be discussed later)
Arrays of objects are allowed (stored contiguously). Objects
can be used as members of some other class in nested fashion.
Objects, just like built-in types, can be return type of
functions.
Private functions and variables can only be accessed from
within the class.
Important Properties
Invoking other member functions from inside a member
function does not require explicit use of the object
Array size, if used inside classes, need to be determined at
compile time (for dynamic arrays, with size to be determined
at run time, “new” operator is used inside a constructor, will
be discussed later)
Arrays of objects are allowed (stored contiguously). Objects
can be used as members of some other class in nested fashion.
Objects, just like built-in types, can be return type of
functions.
Private functions and variables can only be accessed from
within the class.
Important Properties
Invoking other member functions from inside a member
function does not require explicit use of the object
Array size, if used inside classes, need to be determined at
compile time (for dynamic arrays, with size to be determined
at run time, “new” operator is used inside a constructor, will
be discussed later)
Arrays of objects are allowed (stored contiguously). Objects
can be used as members of some other class in nested fashion.
Objects, just like built-in types, can be return type of
functions.
Private functions and variables can only be accessed from
within the class.
Important Properties
Invoking other member functions from inside a member
function does not require explicit use of the object
Array size, if used inside classes, need to be determined at
compile time (for dynamic arrays, with size to be determined
at run time, “new” operator is used inside a constructor, will
be discussed later)
Arrays of objects are allowed (stored contiguously). Objects
can be used as members of some other class in nested fashion.
Objects, just like built-in types, can be return type of
functions.
Private functions and variables can only be accessed from
within the class.
Important Properties
Invoking other member functions from inside a member
function does not require explicit use of the object
Array size, if used inside classes, need to be determined at
compile time (for dynamic arrays, with size to be determined
at run time, “new” operator is used inside a constructor, will
be discussed later)
Arrays of objects are allowed (stored contiguously). Objects
can be used as members of some other class in nested fashion.
Objects, just like built-in types, can be return type of
functions.
Private functions and variables can only be accessed from
within the class.
Classes
Problem
Consider a class “car” which has the carNo, carModel, carMake
fields and relevant functions to modify them.
You have to count the number of objects of the class created.
How do you do it?
Classes
Problem
Consider a class “car” which has the carNo, carModel, carMake
fields and relevant functions to modify them.
You have to count the number of objects of the class created.
How do you do it?
Classes
Problem
Consider a class “car” which has the carNo, carModel, carMake
fields and relevant functions to modify them.
You have to count the number of objects of the class created.
How do you do it?
Static Members
class item {
static int count;
// rest of class definition
};
int item :: count;
Static Members
Properties
Every static member needs to be defined outside the class as
well.
Only one copy of the static variable is shared among all objects
of the class.
Visible only within the class but exists for the lifetime of the
program.
No object instantiation is required to access static members.
Static Members
Properties
Every static member needs to be defined outside the class as
well.
Only one copy of the static variable is shared among all objects
of the class.
Visible only within the class but exists for the lifetime of the
program.
No object instantiation is required to access static members.
Static Members
Properties
Every static member needs to be defined outside the class as
well.
Only one copy of the static variable is shared among all objects
of the class.
Visible only within the class but exists for the lifetime of the
program.
No object instantiation is required to access static members.
Static Members
Properties
Every static member needs to be defined outside the class as
well.
Only one copy of the static variable is shared among all objects
of the class.
Visible only within the class but exists for the lifetime of the
program.
No object instantiation is required to access static members.
Static Members
Properties
Every static member needs to be defined outside the class as
well.
Only one copy of the static variable is shared among all objects
of the class.
Visible only within the class but exists for the lifetime of the
program.
No object instantiation is required to access static members.
Memory Allocation of Objects
For member functions and static variables, memory is allocated
when the class is defined, all objects of the class share the
same function code and static variables (every class does not
get its own copy). “this” pointer is implicitly passed so that
the function code is executed on the correct class instance.
For variables, memory is allocated when the object is defined.
Each instance gets its own set of variables.
Memory Allocation of Objects
For member functions and static variables, memory is allocated
when the class is defined, all objects of the class share the
same function code and static variables (every class does not
get its own copy). “this” pointer is implicitly passed so that
the function code is executed on the correct class instance.
For variables, memory is allocated when the object is defined.
Each instance gets its own set of variables.
Class: Friend
Shared functions among multiple classes
Properties:
Can access private members of the class
Often used in operator overloading
Not in the scope of the class. Cannot be called using an object
of the class.
Can be invoked like a normal function
Cannot access members of a class directly without an object of
the class
Class: Friend
Shared functions among multiple classes
Properties:
Can access private members of the class
Often used in operator overloading
Not in the scope of the class. Cannot be called using an object
of the class.
Can be invoked like a normal function
Cannot access members of a class directly without an object of
the class
Class: Friend
Shared functions among multiple classes
Properties:
Can access private members of the class
Often used in operator overloading
Not in the scope of the class. Cannot be called using an object
of the class.
Can be invoked like a normal function
Cannot access members of a class directly without an object of
the class
Class: Friend
Shared functions among multiple classes
Properties:
Can access private members of the class
Often used in operator overloading
Not in the scope of the class. Cannot be called using an object
of the class.
Can be invoked like a normal function
Cannot access members of a class directly without an object of
the class
Class: Friend
Shared functions among multiple classes
Properties:
Can access private members of the class
Often used in operator overloading
Not in the scope of the class. Cannot be called using an object
of the class.
Can be invoked like a normal function
Cannot access members of a class directly without an object of
the class
Class: Friend
Shared functions among multiple classes
Properties:
Can access private members of the class
Often used in operator overloading
Not in the scope of the class. Cannot be called using an object
of the class.
Can be invoked like a normal function
Cannot access members of a class directly without an object of
the class
Class: Friend
Shared functions among multiple classes
Properties:
Can access private members of the class
Often used in operator overloading
Not in the scope of the class. Cannot be called using an object
of the class.
Can be invoked like a normal function
Cannot access members of a class directly without an object of
the class
Class: Friend
Properties: (cont. . . )
Can be declared as either private or public without changing
the meaning
Objects can be passed to the function by value or by reference
A class can be defined to be the friend of another class. In
that case, all member functions of one class are friends of the
other class
Class: Friend
Properties: (cont. . . )
Can be declared as either private or public without changing
the meaning
Objects can be passed to the function by value or by reference
A class can be defined to be the friend of another class. In
that case, all member functions of one class are friends of the
other class
Class: Friend
Properties: (cont. . . )
Can be declared as either private or public without changing
the meaning
Objects can be passed to the function by value or by reference
A class can be defined to be the friend of another class. In
that case, all member functions of one class are friends of the
other class
Class: Friend
Properties: (cont. . . )
Can be declared as either private or public without changing
the meaning
Objects can be passed to the function by value or by reference
A class can be defined to be the friend of another class. In
that case, all member functions of one class are friends of the
other class
References
The C++ Programming Language
- Bjarne Stroustrup
Object Oriented Programming with C++
- E Balaguruswamy
http://www.cplusplus.com/reference
http://www.learncpp.com/
http://www.java2s.com/Code/Cpp/CatalogCpp.htm

Mais conteúdo relacionado

Mais procurados

Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharpMody Farouk
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpsinghadarsh
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpg_hemanth17
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpRaga Vahini
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objectsMahmoud Alfarra
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSaharsh Anand
 

Mais procurados (19)

Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
C#ppt
C#pptC#ppt
C#ppt
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 

Destaque

JavaScript, the good parts + syntactic sugar = CoffeeScript
JavaScript, the good parts + syntactic sugar = CoffeeScript JavaScript, the good parts + syntactic sugar = CoffeeScript
JavaScript, the good parts + syntactic sugar = CoffeeScript geekQ
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template LibraryAnirudh Raja
 
Front End Development - Beyond Javascript (Robin Cannon)
Front End Development - Beyond Javascript (Robin Cannon)Front End Development - Beyond Javascript (Robin Cannon)
Front End Development - Beyond Javascript (Robin Cannon)Future Insights
 
Top 10 things a fresh programmer should know - Dao Ngoc Khanh
Top 10 things a fresh programmer should know - Dao Ngoc KhanhTop 10 things a fresh programmer should know - Dao Ngoc Khanh
Top 10 things a fresh programmer should know - Dao Ngoc KhanhDevDay.org
 
02 greedy, d&c, binary search
02 greedy, d&c, binary search02 greedy, d&c, binary search
02 greedy, d&c, binary searchPankaj Prateek
 
Security in the Internet of Things
Security in the Internet of ThingsSecurity in the Internet of Things
Security in the Internet of ThingsForgeRock
 
Becoming a Better Programmer
Becoming a Better ProgrammerBecoming a Better Programmer
Becoming a Better ProgrammerPete Goodliffe
 

Destaque (12)

03 dp
03 dp03 dp
03 dp
 
05 graph
05 graph05 graph
05 graph
 
JavaScript, the good parts + syntactic sugar = CoffeeScript
JavaScript, the good parts + syntactic sugar = CoffeeScript JavaScript, the good parts + syntactic sugar = CoffeeScript
JavaScript, the good parts + syntactic sugar = CoffeeScript
 
01 basics and stl
01 basics and stl01 basics and stl
01 basics and stl
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
 
04 maths
04 maths04 maths
04 maths
 
Front End Development - Beyond Javascript (Robin Cannon)
Front End Development - Beyond Javascript (Robin Cannon)Front End Development - Beyond Javascript (Robin Cannon)
Front End Development - Beyond Javascript (Robin Cannon)
 
Top 10 things a fresh programmer should know - Dao Ngoc Khanh
Top 10 things a fresh programmer should know - Dao Ngoc KhanhTop 10 things a fresh programmer should know - Dao Ngoc Khanh
Top 10 things a fresh programmer should know - Dao Ngoc Khanh
 
02 greedy, d&c, binary search
02 greedy, d&c, binary search02 greedy, d&c, binary search
02 greedy, d&c, binary search
 
Security in the Internet of Things
Security in the Internet of ThingsSecurity in the Internet of Things
Security in the Internet of Things
 
Becoming a Better Programmer
Becoming a Better ProgrammerBecoming a Better Programmer
Becoming a Better Programmer
 

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

Semelhante a Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK (20)

struct and class deferences
 struct and class deferences struct and class deferences
struct and class deferences
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
 
oopusingc.pptx
oopusingc.pptxoopusingc.pptx
oopusingc.pptx
 
C questions
C questionsC questions
C questions
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Structures
StructuresStructures
Structures
 

Último

(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Último (20)

DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 

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

  • 1. ACA Summer School 2014 Advanced C++ Pankaj Prateek ACA, CSE, IIT Kanpur June 23, 2014
  • 2. Course website: http: //www.cse.iitk.ac.in/users/aca/sumschool2014.html Evaluation End Sem - 50% Assignments / In-class quiz - 50% Timings: M-F 1430 - 1600 hrs
  • 3. Prerequisites A good command over any programming language preferably C/C++ Pointers Structures
  • 4. Structures How do you store the details of a person? char strName [20]; int nBirthYear; int nBirthMonth; int nBirthDay; int nHeight; Information is not grouped in any way. To pass your information to a function, you would have to pass each variable independently. If wanted to store information about many people, would have to declare arrays.
  • 5. Structures How do you store the details of a person? char strName [20]; int nBirthYear; int nBirthMonth; int nBirthDay; int nHeight; Information is not grouped in any way. To pass your information to a function, you would have to pass each variable independently. If wanted to store information about many people, would have to declare arrays.
  • 6. Structures How do you store the details of a person? char strName [20]; int nBirthYear; int nBirthMonth; int nBirthDay; int nHeight; Information is not grouped in any way. To pass your information to a function, you would have to pass each variable independently. If wanted to store information about many people, would have to declare arrays.
  • 7. Structures How do you store the details of a person? char strName [20]; int nBirthYear; int nBirthMonth; int nBirthDay; int nHeight; Information is not grouped in any way. To pass your information to a function, you would have to pass each variable independently. If wanted to store information about many people, would have to declare arrays.
  • 8. Structures How do you store the details of a person? char strName [20]; int nBirthYear; int nBirthMonth; int nBirthDay; int nHeight; Information is not grouped in any way. To pass your information to a function, you would have to pass each variable independently. If wanted to store information about many people, would have to declare arrays.
  • 9. Structures C++ allows to create own user-defined data types to aggregate different variables : structs Structure declaration struct Person{ char strName [20]; int nBirthYear; int nBirthMonth; int nBirthDay; int nHeight; }; //DO NOT forget a semicolon Now person structure can be used as a built-in variable.
  • 10. Structures C++ allows to create own user-defined data types to aggregate different variables : structs Structure declaration struct Person{ char strName [20]; int nBirthYear; int nBirthMonth; int nBirthDay; int nHeight; }; //DO NOT forget a semicolon Now person structure can be used as a built-in variable.
  • 11. Structures C++ allows to create own user-defined data types to aggregate different variables : structs Structure declaration struct Person{ char strName [20]; int nBirthYear; int nBirthMonth; int nBirthDay; int nHeight; }; //DO NOT forget a semicolon Now person structure can be used as a built-in variable.
  • 12. Structures Usage struct Person p1; struct Person p2; Accessing Members: p1.name = ‘‘pankaj ’’; p1.nBirthDay = 20; p2.name = ‘‘Rahul ’’;
  • 13. Structures Usage struct Person p1; struct Person p2; Accessing Members: p1.name = ‘‘pankaj ’’; p1.nBirthDay = 20; p2.name = ‘‘Rahul ’’;
  • 14. Structures No memory is allocated in structure declaration Size of a structure is the sum of sizes of it elements 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); }
  • 15. Structures No memory is allocated in structure declaration Size of a structure is the sum of sizes of it elements 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); }
  • 16. Structures No memory is allocated in structure declaration Size of a structure is the sum of sizes of it elements 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); }
  • 17. Structures Always have to use the word “struct” No explicit connection between members of a structure and the functions manipulating them Cannot be treated as built-in types (c1 + c2 is not valid for instances of “struct complex”) Data hiding is not permitted (Why do we need this?) All members of a structure are by defaut “public” (will be discussed later)
  • 18. Structures Always have to use the word “struct” No explicit connection between members of a structure and the functions manipulating them Cannot be treated as built-in types (c1 + c2 is not valid for instances of “struct complex”) Data hiding is not permitted (Why do we need this?) All members of a structure are by defaut “public” (will be discussed later)
  • 19. Structures Always have to use the word “struct” No explicit connection between members of a structure and the functions manipulating them Cannot be treated as built-in types (c1 + c2 is not valid for instances of “struct complex”) Data hiding is not permitted (Why do we need this?) All members of a structure are by defaut “public” (will be discussed later)
  • 20. Structures Always have to use the word “struct” No explicit connection between members of a structure and the functions manipulating them Cannot be treated as built-in types (c1 + c2 is not valid for instances of “struct complex”) Data hiding is not permitted (Why do we need this?) All members of a structure are by defaut “public” (will be discussed later)
  • 21. Structures Always have to use the word “struct” No explicit connection between members of a structure and the functions manipulating them Cannot be treated as built-in types (c1 + c2 is not valid for instances of “struct complex”) Data hiding is not permitted (Why do we need this?) All members of a structure are by defaut “public” (will be discussed later)
  • 22. Classes Extension of structures Class Definition class class_name { private: variable declarations; function declarations; public: variable declarations; function declarations; }; //DO NOT forget the semicolon
  • 23. Classes Extension of structures Class Definition class class_name { private: variable declarations; function declarations; public: variable declarations; function declarations; }; //DO NOT forget the semicolon
  • 24. Class: Example Example class item { private: int number; // variable declarations float cost; // private by default! public: // function declarations through prototypes void getData(int a, float b); void putData(void ); }
  • 25. Class No memory allocated for a class definiton. It is only a “template”, like a definition of a structure
  • 26. Class: Public and Private Private members are not directly accessible outside the class. They are “hidden” from the outside world. The only way to access them is by using public functions (if defined) which manipulate them. Public members can be accessed using the dot operator (member selection operator). Eg. student.name, item.getData()
  • 27. Class: Public and Private Private members are not directly accessible outside the class. They are “hidden” from the outside world. The only way to access them is by using public functions (if defined) which manipulate them. Public members can be accessed using the dot operator (member selection operator). Eg. student.name, item.getData()
  • 28. Class: Objects Class, like a structure definition, is a user-defined data type without any concrete existance. A concrete instance of a class is an object. Memory is allocated for every object that is created. An independent new set of variables is created for each object. Public members can be accessed by using the dot operator on the object while private members cannot be accessed directly.
  • 29. Class: Objects Class, like a structure definition, is a user-defined data type without any concrete existance. A concrete instance of a class is an object. Memory is allocated for every object that is created. An independent new set of variables is created for each object. Public members can be accessed by using the dot operator on the object while private members cannot be accessed directly.
  • 30. Class: Objects Class, like a structure definition, is a user-defined data type without any concrete existance. A concrete instance of a class is an object. Memory is allocated for every object that is created. An independent new set of variables is created for each object. Public members can be accessed by using the dot operator on the object while private members cannot be accessed directly.
  • 31. Class: Objects Class, like a structure definition, is a user-defined data type without any concrete existance. A concrete instance of a class is an object. Memory is allocated for every object that is created. An independent new set of variables is created for each object. Public members can be accessed by using the dot operator on the object while private members cannot be accessed directly.
  • 32. Class: Objects Class, like a structure definition, is a user-defined data type without any concrete existance. A concrete instance of a class is an object. Memory is allocated for every object that is created. An independent new set of variables is created for each object. Public members can be accessed by using the dot operator on the object while private members cannot be accessed directly.
  • 33. Class: Objects Object Creation Just like variable declaration Memory is allocated for every object that is created Example: item a; item b,c,d;
  • 34. Class: Objects Object Creation Just like variable declaration Memory is allocated for every object that is created Example: item a; item b,c,d;
  • 35. Class: Objects Object Creation Just like variable declaration Memory is allocated for every object that is created Example: item a; item b,c,d;
  • 36. Class: Objects Object Creation Just like variable declaration Memory is allocated for every object that is created Example: item a; item b,c,d;
  • 37. 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; }
  • 38. 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; }
  • 39. Class: Function definitions Inside the class class Employee { int empno , salary; public: void set(int roll , int sal) { empno = roll; salary = sal; } };
  • 40. Class: Function definitions Inside the class class Employee { int empno , salary; public: void set(int roll , int sal) { empno = roll; salary = sal; } };
  • 41. Important Properties Invoking other member functions from inside a member function does not require explicit use of the object Array size, if used inside classes, need to be determined at compile time (for dynamic arrays, with size to be determined at run time, “new” operator is used inside a constructor, will be discussed later) Arrays of objects are allowed (stored contiguously). Objects can be used as members of some other class in nested fashion. Objects, just like built-in types, can be return type of functions. Private functions and variables can only be accessed from within the class.
  • 42. Important Properties Invoking other member functions from inside a member function does not require explicit use of the object Array size, if used inside classes, need to be determined at compile time (for dynamic arrays, with size to be determined at run time, “new” operator is used inside a constructor, will be discussed later) Arrays of objects are allowed (stored contiguously). Objects can be used as members of some other class in nested fashion. Objects, just like built-in types, can be return type of functions. Private functions and variables can only be accessed from within the class.
  • 43. Important Properties Invoking other member functions from inside a member function does not require explicit use of the object Array size, if used inside classes, need to be determined at compile time (for dynamic arrays, with size to be determined at run time, “new” operator is used inside a constructor, will be discussed later) Arrays of objects are allowed (stored contiguously). Objects can be used as members of some other class in nested fashion. Objects, just like built-in types, can be return type of functions. Private functions and variables can only be accessed from within the class.
  • 44. Important Properties Invoking other member functions from inside a member function does not require explicit use of the object Array size, if used inside classes, need to be determined at compile time (for dynamic arrays, with size to be determined at run time, “new” operator is used inside a constructor, will be discussed later) Arrays of objects are allowed (stored contiguously). Objects can be used as members of some other class in nested fashion. Objects, just like built-in types, can be return type of functions. Private functions and variables can only be accessed from within the class.
  • 45. Important Properties Invoking other member functions from inside a member function does not require explicit use of the object Array size, if used inside classes, need to be determined at compile time (for dynamic arrays, with size to be determined at run time, “new” operator is used inside a constructor, will be discussed later) Arrays of objects are allowed (stored contiguously). Objects can be used as members of some other class in nested fashion. Objects, just like built-in types, can be return type of functions. Private functions and variables can only be accessed from within the class.
  • 46. Classes Problem Consider a class “car” which has the carNo, carModel, carMake fields and relevant functions to modify them. You have to count the number of objects of the class created. How do you do it?
  • 47. Classes Problem Consider a class “car” which has the carNo, carModel, carMake fields and relevant functions to modify them. You have to count the number of objects of the class created. How do you do it?
  • 48. Classes Problem Consider a class “car” which has the carNo, carModel, carMake fields and relevant functions to modify them. You have to count the number of objects of the class created. How do you do it?
  • 49. Static Members class item { static int count; // rest of class definition }; int item :: count;
  • 50. Static Members Properties Every static member needs to be defined outside the class as well. Only one copy of the static variable is shared among all objects of the class. Visible only within the class but exists for the lifetime of the program. No object instantiation is required to access static members.
  • 51. Static Members Properties Every static member needs to be defined outside the class as well. Only one copy of the static variable is shared among all objects of the class. Visible only within the class but exists for the lifetime of the program. No object instantiation is required to access static members.
  • 52. Static Members Properties Every static member needs to be defined outside the class as well. Only one copy of the static variable is shared among all objects of the class. Visible only within the class but exists for the lifetime of the program. No object instantiation is required to access static members.
  • 53. Static Members Properties Every static member needs to be defined outside the class as well. Only one copy of the static variable is shared among all objects of the class. Visible only within the class but exists for the lifetime of the program. No object instantiation is required to access static members.
  • 54. Static Members Properties Every static member needs to be defined outside the class as well. Only one copy of the static variable is shared among all objects of the class. Visible only within the class but exists for the lifetime of the program. No object instantiation is required to access static members.
  • 55. Memory Allocation of Objects For member functions and static variables, memory is allocated when the class is defined, all objects of the class share the same function code and static variables (every class does not get its own copy). “this” pointer is implicitly passed so that the function code is executed on the correct class instance. For variables, memory is allocated when the object is defined. Each instance gets its own set of variables.
  • 56. Memory Allocation of Objects For member functions and static variables, memory is allocated when the class is defined, all objects of the class share the same function code and static variables (every class does not get its own copy). “this” pointer is implicitly passed so that the function code is executed on the correct class instance. For variables, memory is allocated when the object is defined. Each instance gets its own set of variables.
  • 57. Class: Friend Shared functions among multiple classes Properties: Can access private members of the class Often used in operator overloading Not in the scope of the class. Cannot be called using an object of the class. Can be invoked like a normal function Cannot access members of a class directly without an object of the class
  • 58. Class: Friend Shared functions among multiple classes Properties: Can access private members of the class Often used in operator overloading Not in the scope of the class. Cannot be called using an object of the class. Can be invoked like a normal function Cannot access members of a class directly without an object of the class
  • 59. Class: Friend Shared functions among multiple classes Properties: Can access private members of the class Often used in operator overloading Not in the scope of the class. Cannot be called using an object of the class. Can be invoked like a normal function Cannot access members of a class directly without an object of the class
  • 60. Class: Friend Shared functions among multiple classes Properties: Can access private members of the class Often used in operator overloading Not in the scope of the class. Cannot be called using an object of the class. Can be invoked like a normal function Cannot access members of a class directly without an object of the class
  • 61. Class: Friend Shared functions among multiple classes Properties: Can access private members of the class Often used in operator overloading Not in the scope of the class. Cannot be called using an object of the class. Can be invoked like a normal function Cannot access members of a class directly without an object of the class
  • 62. Class: Friend Shared functions among multiple classes Properties: Can access private members of the class Often used in operator overloading Not in the scope of the class. Cannot be called using an object of the class. Can be invoked like a normal function Cannot access members of a class directly without an object of the class
  • 63. Class: Friend Shared functions among multiple classes Properties: Can access private members of the class Often used in operator overloading Not in the scope of the class. Cannot be called using an object of the class. Can be invoked like a normal function Cannot access members of a class directly without an object of the class
  • 64. Class: Friend Properties: (cont. . . ) Can be declared as either private or public without changing the meaning Objects can be passed to the function by value or by reference A class can be defined to be the friend of another class. In that case, all member functions of one class are friends of the other class
  • 65. Class: Friend Properties: (cont. . . ) Can be declared as either private or public without changing the meaning Objects can be passed to the function by value or by reference A class can be defined to be the friend of another class. In that case, all member functions of one class are friends of the other class
  • 66. Class: Friend Properties: (cont. . . ) Can be declared as either private or public without changing the meaning Objects can be passed to the function by value or by reference A class can be defined to be the friend of another class. In that case, all member functions of one class are friends of the other class
  • 67. Class: Friend Properties: (cont. . . ) Can be declared as either private or public without changing the meaning Objects can be passed to the function by value or by reference A class can be defined to be the friend of another class. In that case, all member functions of one class are friends of the other class
  • 68. References The C++ Programming Language - Bjarne Stroustrup Object Oriented Programming with C++ - E Balaguruswamy http://www.cplusplus.com/reference http://www.learncpp.com/ http://www.java2s.com/Code/Cpp/CatalogCpp.htm