Post on 04-Jun-2018
transcript
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
1/22
Object oriented programming(OOP)
Lecture No. 7
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
2/22
Class
Class is a tool to realize objects
Class is a tool for defining a new type
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
3/22
Example
Lion is an object
Student is an object
Both has some attributes and somebehaviors
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
4/22
Uses
The problem becomes easy to understand
Interactions can be easily modeled
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
5/22
Type in C++
Mechanism for user defined types are
Structures
Classes
Built-in types are like int, float and double
User defined type can be
Student in student management system Circle in a drawing software
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
6/22
Abstraction
Only include details in the system that arerequired for making a functional system
Student
Name
Address
Sibling
Father Business
Relevant to our problem
Not relevant to our problem
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
7/22
Defining a New User Defined Type
class ClassName
{
DataTypeMemberVariable;
ReturnTypeMemberFunction();
};
Syntax
Syntax
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
8/22
Example
class Student
{
int rollNo;
char *name;
float CGPA;
char *address;
void setName(char *newName);
void setRollNo(int newRollNo);
};
Member variablesMembe
rFunctions
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
9/22
Why Member Function
They model the behaviors of an object
Objects can make their data invisible
Object remains in consistent state
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
10/22
Example
Student aStudent;
aStudent.rollNo = 514;
aStudent.rollNo = -514; //Error
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
11/22
Object and Class
Object is an instantiation of a user definedtype or a class
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
12/22
Declaring class variables
Variables of classes (objects) are declaredjust like variables of structures and built-indata types
TypeName VaraibaleName;
intvar
;Student aStudent;
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
13/22
Accessing members
Members of an object can be accessedusing
dot operator (.) to access via the variable name
arrow operator (->) to access via a pointer toan object
Member variables and member functions
are accessed in a similar fashion
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
14/22
Example
class Student{
int rollNo;
void setRollNo(int
aNo);};
Student aStudent;
aStudent.rollNo;
Error
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
15/22
Access specifiers
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
16/22
Access specifiers
There are three access specifiers
public is used to tell that member can beaccessed whenever you have access to the
objectprivate is used to tell that member can only be
accessed from a member function
protected to be discussed when we coverinheritance
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
17/22
Example
class Student{
private:
char * name;
int rollNo;public:
void setName(char *);
void setRollNo(int);
...
};
Cannot be accessed outside class
Can be
accessed
outside class
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
18/22
Example
class Student{
...
int rollNo;
public:void setRollNo(int aNo);
};
int main(){
Student aStudent;aStudent.SetRollNo(1);
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
19/22
Default access specifiers
When no access specifier is mentioned thenby default the member is considered privatemember
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
20/22
Example
class Student
{
char * name;
int RollNo;
};
class Student
{
private:
char * name;
int RollNo;
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
21/22
Example
class Student
{
char * name;int RollNo;
void SetName(char *);
};Student aStudent;
aStudent.SetName(Ali);
Error
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 07
22/22
Example
class Student
{
char * name;
int RollNo;public:
void setName(char *);
};Student aStudent;
aStudent.SetName(Ali);