Lec 02 Class, Objects and Access Specifiers (1)
Lec 02 Class, Objects and Access Specifiers (1)
Course Code:CS1022
Lecture 3
Lecturer: Sehrish Munawar Cheema
Email: sehrish.munawar@umt.edu.pk
Class
• A class serves as a plan, or template. It specifies what data
and what functions will be included in objects of that class.
• Defining the class doesn’t create any objects, just as the type
int doesn’t create any variables.
• A class is thus a description of a number of similar objects.
• Prince, Sting, and Madonna are members of the class of rock
musicians.
• There is no one person called “rock musician” but specific
people with specific names are members of this class if they
possess certain characteristics.
Objects
• Look around right now and you'll find many
examples of real-world objects:
• your laptop, your television set, your bicycle.
• Real-world objects share two characteristics: They
all have
• State and
• Behavior
• Your bicycle also have state (current gear, current
pedal cadence, current speed) and behavior
(changing gear, changing pedal cadence, applying
brakes).
DECLARING CLASS VARIABLES
Variables of classes (objects) are declared just like
variables of structures and built-in data types as follows,
TypeName VariableName;
int var; // declaring built in int data type
variable
Student aStudent; // declaring user defined class
Student object
A ‘C++’ CLASS
• In C++:
Data members(Member variables):
variables which are used(declared inside a
class) to represent the attributes of a class.
Member functions: functions which are used
to define the behaviors of a class.
• A C++ class has two parts:
Interface of a Class
Implementation of a Class
THE INTERFACE
The interface of a Class like a manual
Describes what the objects of this class can
do for us, and also how to request these
services from objects.
Allow compiler to recognize the classes when
used elsewhere.
The interface includes
– Data Members
– Member Function Prototypes
EXAMPLE: STUDENT CLASS
Data Member: int student_name;
Data Member: int rNoll;
Member function: void getstudentdata();
class studentRecord
{
public: void getstudentdata();
private: int courseNumber;
int student_name;
};
ACCESSING MEMBERS
Members of an object can be accessed using,
1. Dot operator (.) to access via the variable name
Student aStudent; // declaring Student object
aStudent. rollNo = 5;
2. Arrow operator (->) to access via a pointer to an
object
Student * aStudent = new Student();
// declaring and initializing Student pointer
aStudent->rollNo= 5;
Member functions are accessed in the similar
way using dot or arrow operator.
ACCESS SPECIFIERS
These are used to enforce access restrictions to members of a
class, there are three access specifiers,
public
is used to allow the user that member can be accessed within
the class and outside the class.
is used to tell that member can be accessed whenever you have
access to the object
private
is used to restrict the use of class member within the class.
is used to tell that member can only be accessed from a member
function
protected
to be discussed when we cover inheritance
PUBLIC: OR PRIVATE:
It is not compulsory for all members
functions to be public and all data
members to be private.
Some member functions can also be
private.
Similarly, data members can also be
public.
A SIMPLE CLASS (1)
#include <iostream>
using namespace std;
class smallobj //declare a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{
somedata = d;
}
void showdata() //member function to display data
{
cout << “Data is ” << somedata << endl;
}
};
A SIMPLE CLASS (2)
int main()
{
smallobj s1, s2; //define two objects of class
smallobj
s1.setdata(1066); //call member function to set data
s2.setdata(1776);
s1.showdata(); //call member function to display
data
s2.showdata();
return 0;
}
CLASS AND OBJECTS
Object is an instantiation of a user defined
type or class. Once we have defined a
class we can create as many objects for
that class as we require.
We define two objects s1 and s2 that are
instances of that class.
DECLARING THE CLASS (1)
• declaration starts with the keyword class.
• Like a structure, the body of the class is
delimited by braces and terminated by a
semicolon.
PRIVATE AND PUBLIC
• Body of the class contains two unfamiliar
keywords private and public.
• key feature of object–oriented
programming is data hiding
• Private not accessible from outside class.
• Public accessible from outside class.
CLASS DATA