View
229
Download
0
Category
Preview:
Citation preview
8/3/2019 Computer Notes - Functions & Constructor
1/38
Functions & ConstructorFunctions & Constructor
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
2/38
ReviewReview
ClassClass
ConceptConcept
DefinitionDefinition
Data membersData membersMember FunctionsMember Functions
Access specifierAccess specifier
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
3/38
Member FunctionsMember Functions
Member functions are the functions that operateMember functions are the functions that operate
on the data encapsulated in the classon the data encapsulated in the class
Public member functions are the interface to thePublic member functions are the interface to the
classclass
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
4/38
Member Functions (contd.)Member Functions (contd.)
Define member function inside the classDefine member function inside the class
definitiondefinition
OROR
Define member function outside the classDefine member function outside the classdefinitiondefinition
But they must be declared inside class definitionBut they must be declared inside class definition
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
5/38
Function Inside Class BodyFunction Inside Class Body
classclass ClassNameClassName {{
public:public:
ReturnTypeReturnType FunctionNameFunctionName() {() {
}}};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
6/38
ExampleExample
Define a class of student that hasDefine a class of student that hasa roll number. This class shoulda roll number. This class should
have a function that can be usedhave a function that can be usedto set the roll numberto set the roll number
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
7/38
ExampleExampleclass Student{class Student{
intint rollNorollNo;;
public:public:
voidvoidsetRollNo(intsetRollNo(int aRollNoaRollNo){){rollNorollNo == aRollNoaRollNo;;
}}
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
8/38
Function Outside Class BodyFunction Outside Class Bodyclassclass ClassNameClassName{{
public:public:
ReturnTypeReturnType FunctionNameFunctionName();();
};};
ReturnTypeReturnType ClassNameClassName::::FunctionNameFunctionName()()
{{
}}Scope resolution
operator
8/3/2019 Computer Notes - Functions & Constructor
9/38
ExampleExampleclass Student{class Student{
intint rollNorollNo;;
public:public:
voidvoidsetRollNo(intsetRollNo(int aRollNoaRollNo););};};
voidvoidStudent::setRollNo(intStudent::setRollNo(int aRollNoaRollNo){){
rollNorollNo == aRollNoaRollNo;;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
10/38
Inline FunctionsInline Functions
Instead of calling an inline function compilerInstead of calling an inline function compiler
replaces the code at the function call pointreplaces the code at the function call point
KeywordKeywordinlineinlineis used to request compiler tois used to request compiler to
make a function inlinemake a function inlineIt is a request and not a commandIt is a request and not a command
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
11/38
ExampleExampleinlineinline intintArea(intArea(int lenlen,, intint hi)hi)
{{returnreturn lenlen * hi;* hi;
}}
intintmain()main()
{{
coutcout
8/3/2019 Computer Notes - Functions & Constructor
12/38
Inline FunctionsInline Functions
If we define the function inside the class bodyIf we define the function inside the class body
then the function is by default an inline functionthen the function is by default an inline function
In case function is defined outside the classIn case function is defined outside the class
body then we must use the keywordbody then we must use the keywordinlineinlinetotomake a function inlinemake a function inline
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
13/38
ExampleExampleclass Student{class Student{
intint rollNorollNo;;public:public:
voidvoid
setRollNo(intsetRollNo(int
aRollNoaRollNo
){){
rollNorollNo == aRollNoaRollNo;;
}}};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
14/38
ExampleExampleclass Student{class Student{
public:public:
inline voidinline voidsetRollNo(intsetRollNo(int aRollNoaRollNo););
};};
voidvoidStudent::setRollNo(intStudent::setRollNo(int aRollNoaRollNo){){
rollNorollNo == aRollNoaRollNo;;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
15/38
ExampleExampleclass Student{class Student{
public:public:
voidvoidsetRollNo(intsetRollNo(int aRollNoaRollNo););
};};inline voidinline voidStudent::setRollNo(intStudent::setRollNo(int
aRollNoaRollNo){){
rollNorollNo == aRollNoaRollNo;;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
16/38
ExampleExampleclass Student{class Student{
public:public:
inline voidinline voidsetRollNo(intsetRollNo(int aRollNoaRollNo););
};};inline voidinline voidStudent::setRollNo(intStudent::setRollNo(int
aRollNoaRollNo){){
rollNorollNo == aRollNoaRollNo;;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
17/38
ConstructorConstructor
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
18/38
ConstructorConstructorConstructor is used to initialize the objects of aConstructor is used to initialize the objects of a
classclassConstructor is used to ensure that object is inConstructor is used to ensure that object is in
well defined state at the time of creationwell defined state at the time of creation
Constructor is automatically called when theConstructor is automatically called when the
object is createdobject is created
Constructor are not usually called explicitlyConstructor are not usually called explicitly
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
19/38
Constructor (contd.)Constructor (contd.)Constructor is a special function having sameConstructor is a special function having same
name as the class namename as the class nameConstructor does not have return typeConstructor does not have return type
Constructors are commonly public membersConstructors are commonly public members
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
20/38
ExampleExampleclass Student{class Student{
public:public:
Student(){Student(){
rollNorollNo = 0;= 0;
}}};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
21/38
ExampleExampleintintmain()main()
{{
StudentStudent aStudentaStudent;;
/*constructor is implicitly called/*constructor is implicitly calledat this point*/at this point*/
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
22/38
Default ConstructorDefault ConstructorConstructor without any argument is calledConstructor without any argument is called
default constructordefault constructorIf we do not define a default constructor theIf we do not define a default constructor the
compiler will generate a default constructorcompiler will generate a default constructor
This compiler generated default constructorThis compiler generated default constructor
initialize the data members to their defaultinitialize the data members to their default
valuesvalues
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
23/38
ExampleExampleclass Studentclass Student
{{intint rollNorollNo;;
char *name;char *name;
float GPA;float GPA;
public:public:
//no constructors//no constructors};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
24/38
ExampleExample
Compiler generated default constructorCompiler generated default constructor
{{
rollNorollNo = 0;= 0;
GPA = 0.0;GPA = 0.0;
name = NULL;name = NULL;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
25/38
Constructor OverloadingConstructor OverloadingConstructors can have parametersConstructors can have parameters
These parameters are used to initialize the dataThese parameters are used to initialize the datamembers with user supplied datamembers with user supplied data
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
26/38
ExampleExampleclass Student{class Student{
public:public:
Student();Student();
Student(char *Student(char * aNameaName););Student(char *Student(char * aNameaName,, intint aRollNoaRollNo););
Student(intStudent(int aRollNoaRollNo,, intint aRollNoaRollNo, float, float
aGPAaGPA););};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
27/38
ExampleExampleStudent::Student(Student::Student(intint aRollNoaRollNo,,
char *char * aNameaName){){if(aRollNoif(aRollNo< 0){< 0){
rollNorollNo = 0;= 0;
}}else {else {
rollNorollNo == aRollNoaRollNo;;
}}
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
28/38
ExampleExampleintintmain()main()
{{
Student student1;Student student1;
Student student2(Student student2(NameName););Student student3(Student student3(NameName, 1);, 1);
Student student4(Student student4(NameName,1,4.0);,1,4.0);
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
29/38
Constructor OverloadingConstructor OverloadingUse default parameter value to reduce theUse default parameter value to reduce the
writing effortwriting effort
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
30/38
ExampleExampleStudent::Student(Student::Student( char *char * aNameaName = NULL,= NULL,
intint aRollNoaRollNo= 0,= 0,
floatfloat aGPAaGPA= 0.0){= 0.0){
}}
Is equivalent toIs equivalent toStudent();Student();
Student(char *Student(char * aNameaName););
Student(char *Student(char * aNameaName,, intint aRollNoaRollNo););Student(char * Name,Student(char * Name, intint aRollNoaRollNo, float, float aGPAaGPA););
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
31/38
Copy ConstructorCopy ConstructorCopy constructor are used when:Copy constructor are used when:
Initializing an object at the time of creationInitializing an object at the time of creation
When an object is passed by value to a functionWhen an object is passed by value to a function
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
32/38
ExampleExamplevoid func1(Student student){void func1(Student student){
}}
intintmain(){main(){
StudentStudent studentAstudentA;;
StudentStudent studentBstudentB == studentAstudentA;;
func1(studentA);func1(studentA);}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
33/38
Copy Constructor (Syntax)Copy Constructor (Syntax)Student::Student(Student::Student(
const Student &const Student &objobj){){rollNorollNo == obj.rollNoobj.rollNo;;
name =name = obj.nameobj.name;;
GPA =GPA = obj.GPAobj.GPA;;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
34/38
Shallow CopyShallow CopyWhen we initialize one object with another thenWhen we initialize one object with another then
the compiler copies state of one object to thethe compiler copies state of one object to theotherother
This kind of copying is called shallow copyingThis kind of copying is called shallow copying
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
35/38
ExampleExample
Student studentA;
Student studentB = studentA;
Name
GPA
RollNo
studentA
Name
GPA
RollNo
studentBA
H
M
AD
Memory
8/3/2019 Computer Notes - Functions & Constructor
36/38
Copy Constructor (contd.)Copy Constructor (contd.)Student::Student(Student::Student(
const Student &const Student & objobj){){intint lenlen == strlen(obj.namestrlen(obj.name););
name = new char[len+1]name = new char[len+1]
strcpy(namestrcpy(name,, obj.nameobj.name););
//copy rest of the data members//copy rest of the data members
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
37/38
Copy Constructor (contd.)Copy Constructor (contd.)Copy constructor is normally used to performCopy constructor is normally used to perform
deep copydeep copyIf we do not make a copy constructor then theIf we do not make a copy constructor then the
compiler performs shallow copycompiler performs shallow copy
http://ecomputernotes.com
8/3/2019 Computer Notes - Functions & Constructor
38/38
ExampleExample
Name
GPARollNo
A
Name
GPARollNo
B
AH
M
AD
Memory
A
HM
A
D
Student studentA;Student studentB = studentA;
Recommended