1. Lecture 09
Classes and Objects
Learn about:
i) More info on member access specifiers
ii) Constant Objects and Functions
iii) Array of Objects
iv) Objects as Function Arguments
v) Returning Objects from Functions
1/27
2. Access Specifiers
• What is access specifier?
– It is the one which specifies which member can be accessed at
which place.
– Types of access specifiers : Data Hiding
• There are three namely,
ob1 ob2
– Private
Friend Ob
– Public
» A class's public members can be accessed by any
function in a program. Used as an interface of the class -
Other objects can use it .
– Protected. 2/27
3. Member access specifiers (page164-176)
Kitty friend of Kitty Snoopy
public public
public
private private
private
3/27
4. How to access the members ?
• If the data members of the class is
– Private :
• Cannot access directly outside the class
• How to access ?
– We should use member functions to access it.
– EG : data member ----> int a;
» int main( )
» { object name.member function( argument ) }
– Public :
• can access outside the class
• EG : object name . Datamembername = value;
» ob1.a = 9; 4/27
Example - Next slide
5. A Simple Class
#include <iostream.h>
class Student // class declaration
{ can only be accessed within class
private:
int idNum; //class data members
double gpa;
accessible from outside and within class
public:
void setData(int id, double result)
{ idNum = id;
gpa = result; }
void showData()
{ cout << ”Student Id is ” << idNum << endl;
cout << ”GPA is " << gpa << endl; }
}; 5/27
6. A simple program example
#include <iostream.h> void main()
{
class Student Student s1, s2, s3;
{
s1.setData(10016666, 3.14);
public:
s2.setData(10011776, 3.55);
int idNum;
double gpa;
s3.idNum = 10011886;
void setData(int, double); s3.gpa = 3.22;
void showData();
}; :
:
Okay, because idNum
} and gpa are public.
//Implementation - refer to notes page 1
(Lecture 9). 6/27
7. A simple program example
#include <iostream.h> void main()
{
class Student Student s1, s2, s3;
{
s1.setData(10016666, 3.14);
private:
s2.setData(10011776, 3.55);
int idNum;
double gpa;
s3.idNum = 10011886;
public: s3.gpa = 3.22;
void setData(int, double);
void showData(); :
}; :
Error!!! idNum
} and gpa are private.
//Implementation - refer to notes page 1 7/27
(Lecture 9).
8. Another program example
class student {
private:
char sid[20]; char name[20]; int semester;
int year; float marks[4]; float average;
public:
// …
student (char [], char [], int sem =1 , int yr = 1);
void print( ) ;
void compute_average( )
{ average = (marks[0]+ marks[1]+ marks[2]+
marks[3])/4; }
}; 8/27
9. Another program example
// class function members implementation
default arguments
student :: student (char id[20], char nama[20], int sem =1 , int yr = 1 )
{ strcpy (name, nama); strcpy (sid, id);
year = yr; semester = sem;
}
void student :: print ( )
{ cout << “Student Id:”<<sid<<endl;
cout << “Student Name:”<<name<<endl;
cout << “Semester:”<<semester<<endl;
}
9/27
10. Private
student
members sid name
... average
...
marks
Private members cannot be semester
accessed from outside the class
year
That’s why, we use public
methods to access them
student (char [] id, char [] nama, int sem =1 , int yr = 1)
compute_average(...)
set_marks (...)
void print()
..
void compute_average()
.
..
void set_marks. (float [])
Public members 10/27
11. Another program example
main ()
{ student a_given_student (“9870025”, “Omar”, 2, 2);
// This is instantiation.
//The object a_given_student is created and the constructor is
//automatically called to initialise that data members of a_given_student with
// I want to change his semester to 3
a_given_student. semester = 3 // ILLEGAL
// why? Because semester is a private data member and cannot
//be accessed outside the class student
...
} 11/27
12. Another program example
Then, how can we access semester and change it?
2 options:
Option 1
We must define a new function member (public) to access semester
class student
{ private: main ()
…. {
student a_given_student (“9870025”, “Omar”, 2, 2);
int semester;
…
// I want to change his semester to 3
public: …
a_given_student. set_semester( 3 ) ;
void set_semester(int sem =1)
…
{semester = sem;}
}
…
};
12/27
13. Another program example
Option 2
We must change the access specifier of semester, and make it public.
class student main ()
{ private: {
…. student a_given_student (“9870025”, “Omar”, 2, 2);
public : int semester;
… // I want to change his semester to 3
public: … a_given_student.semester = 3;
void set_semester(int sem =1) …
{semester = sem;} }
...};
no need!
13/27
14. Constant Object
• Some objects need to be modifiable and some do not.
• The keyword const is used to specify an object is not modifiable, and any
attempt to change the object will produce a syntax error. For example
const Model m1; or Model const m1;
• const objects can only invoke their const member functions.
void showInfo() const
{ ……. }
• const declaration is not required for constructors and destructors of const
objects
Model () const
{ ……… }
14/27
15. Constant objects
#include <iostream.h> void main()
class Part {
{ private: Part part1(8678, 222, 34.55);
int modelnumber; int partnumber Part const part2;
double cost; part2.setpart(2345, 444, 99.90);
public:
Part(int mn, int pn, double c); part1.showpart();
part2.showpart()
void setpart(int mn, int pn, double c)
const; }
void showpart() const;
};
// Implementation - refer to page 5
Lecture 9 15/27
16. Constant objects
class Student
{private: int age;
int semester;
int year;
public:
Student (int, int , int); // constructor
void set_semester(int sem =1){semester=sem;}
//to set semester with sem (1 by default)
void print( ) const;
};
// print is a constant function member
// meaning, it cannot modify any of 16/27
// the data members
17. Constant objects
Student::Student (int sem=5, int ag=20, int yr =1)
{ age=ag; year = yr; semester = sem; }
void Student::print( ) const
{ cout <<"AGE:"<<age<<endl
<<"Year:"<<year<<endl<<"semester:"<<semester<< endl;
semester = 10; // illegal
}
We cannot set any data member with a value
why?
Cause print is const and therefore can just access data members
without altering their values
17/27
18. Constant objects
int main() Normally invalid
{ why?
student h1;
const student h2 (4,4,4); // or student const h2(4,4,4);
h1.set_semester(5); // valid
h2.print(); // valid
h2.set_semester(5);
h2.print();
h1.print(); Cause we choose h2 as a constant object
} SO, there should be no member function
that can alter its data members
EXCEPTION, constructors can still be used by constant objects
18/27
19. Constant objects
• Constructors and destructors are inherently non constant function
members
• Choose your function members that just query data members
to be constants
Syntax:
return_type function_name (type1 par1, ..) const
{… }
• If you choose to work with constant objects, normally all (but
constructor and destructor) your function members should be
constants 19/27
20. Array of Objects (page87-88)
class Student Student
{ private:
sid name
char sid[20]; ... ...
char name[20]; sem
int sem;
Student(...)
public: void print()
Student (char [], char [], int);
void print( ) ; void set_semester(int)
.
void set_semester(int s ) .
.
{ sem = s;}
};
20/27
21. int main ()
{
student FIT[3]; // declares an array called FIT with three elements
// the elements are objects of type (class) student
FIT[1].set_semester(2); // access the 2nd object of the array
// and invokes its method member to set
// its data member
...
}
21/27
22. Array Name
FIT
index
0 1 2
sid name sid name sid name
... ... ... ... ... ...
sem sem 3 sem
student(...) student(...) student(...)
void print() void print() void print()
void set_semester() void set_semester() void set_semester()
1st array element 2nd array element 3rd array element
I want to set the semester of the 2nd array element with 3
22/27
FIT[1].set_semester(3)