computer notes - class
TRANSCRIPT
-
8/3/2019 Computer Notes - Class
1/22
ClassClass
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
2/22
ClassClass
Class is a tool toClass is a tool to reSmithzereSmithze objectsobjects
Class is a tool for defining a new typeClass is a tool for defining a new type
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
3/22
ExampleExample
Lion is an objectLion is an object
Student is an objectStudent is an object
Both has some attributes and some behaviorsBoth has some attributes and some behaviors
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
4/22
UsesUses
The problem becomes easy to understandThe problem becomes easy to understand
Interactions can be easily modeledInteractions can be easily modeled
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
5/22
Type in C++Type in C++
Mechanism for user defined types areMechanism for user defined types are
StructuresStructures
ClassesClasses
BuiltBuilt--in types are likein types are like intint, float and double, float and doubleUser defined type can beUser defined type can be
Student in student management systemStudent in student management system
Circle in a drawing softwareCircle in a drawing software
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
6/22
AbstractionAbstraction
Only include details in the system that areOnly include details in the system that are
required for making a functional systemrequired for making a functional system
StudentStudent
NameNameAddressAddress
SiblingSibling
Father BusinessFather Business
Relevant to our problem
Not relevant to our problem
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
7/22
Defining a New User Defined TypeDefining a New User Defined Type
classclass ClassNameClassName
{{
DataTypeDataType MemberVariableMemberVariable;;
ReturnTypeReturnType MemberFunctionMemberFunction();();
};};
Syntax
Syntax
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
8/22
ExampleExampleclass Studentclass Student
{{intint rollNorollNo;;
char *name;char *name;
float CGPA;float CGPA;
char *address;char *address;
voidvoidsetName(charsetName(char **newNamenewName););
voidvoidsetRollNo(intsetRollNo(int newRollNonewRollNo););
};};
Member variables
Member
Functions
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
9/22
Why Member FunctionWhy Member Function
They model the behaviors of an objectThey model the behaviors of an object
Objects can make their data invisibleObjects can make their data invisible
Object remains in consistent stateObject remains in consistent state
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
10/22
ExampleExample
StudentStudent aStudentaStudent;;
aStudent.rollNoaStudent.rollNo = 514;= 514;
aStudent.rollNoaStudent.rollNo == --514;514; //Error//Error
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
11/22
Object and ClassObject and Class
Object is an instantiation of a user defined typeObject is an instantiation of a user defined type
or a classor a class
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
12/22
Declaring class variablesDeclaring class variables
Variables of classes (objects) are declared justVariables of classes (objects) are declared just
like variables of structures and builtlike variables of structures and built--in datain datatypestypes
TypeNameTypeNameVaraibaleNameVaraibaleName;;
intint varvar;;
StudentStudent aStudentaStudent;;
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
13/22
Accessing membersAccessing members
Members of an object can be accessed usingMembers of an object can be accessed using
dot operator (.) to access via the variable namedot operator (.) to access via the variable name
arrow operator (arrow operator (-->) to access via a pointer to an>) to access via a pointer to an
objectobject
Member variables and member functions areMember variables and member functions are
accessed in a similar fashionaccessed in a similar fashion
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
14/22
ExampleExampleclass Student{class Student{
intint rollNorollNo;;voidvoidsetRollNo(intsetRollNo(int
aNoaNo););
};};
StudentStudent aStudentaStudent;;
aStudent.rollNoaStudent.rollNo;;
Error
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
15/22
Access specifiersAccess specifiers
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
16/22
Access specifiersAccess specifiersThere are three access specifiersThere are three access specifiers
publicpublicis used to tell that member can be accessedis used to tell that member can be accessedwhenever you have access to the objectwhenever you have access to the object
privateprivateis used to tell that member can only beis used to tell that member can only be
accessed from a member functionaccessed from a member function
protectedprotectedto be discussed when we coverto be discussed when we cover
inheritanceinheritance
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
17/22
ExampleExampleclass Student{class Student{
private:private:char * name;char * name;
intint rollNorollNo;;
public:public:voidvoidsetName(charsetName(char *);*);
voidvoidsetRollNo(intsetRollNo(int););
......
};};
Cannot be accessed outside class
Can be accessed
outside class
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
18/22
ExampleExampleclass Student{class Student{
......intint rollNorollNo;;
public:public:
voidvoidsetRollNo(intsetRollNo(int aNoaNo););
};};
intint main(){main(){
StudentStudent aStudentaStudent;;
aStudent.SetRollNo(1);aStudent.SetRollNo(1);
}}
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
19/22
Default access specifiersDefault access specifiersWhen no access specifier is mentioned then byWhen no access specifier is mentioned then by
default the member is considered privatedefault the member is considered privatemembermember
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
20/22
ExampleExampleclass Studentclass Student
{{
char * name;char * name;
intint RollNoRollNo;;
};};
class Studentclass Student
{{
private:private:
char * name;char * name;
intint RollNoRollNo;;
};};
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Class
21/22
ExampleExampleclass Studentclass Student
{{
char * name;char * name;
intint RollNoRollNo;;voidvoidSetName(charSetName(char *);*);
};};
StudentStudent aStudentaStudent;;
aStudent.SetName(Smith);aStudent.SetName(Smith);
Error
-
8/3/2019 Computer Notes - Class
22/22
ExampleExampleclass Studentclass Student
{{char * name;char * name;
intint RollNoRollNo;;
public:public:
voidvoidsetName(charsetName(char *);*);
};};
StudentStudent aStudentaStudent;;
aStudent.SetName(aStudent.SetName(SmithSmith););
http://ecomputernotes.com