0% found this document useful (0 votes)
3 views10 pages

Understanding Classes in OOP

Uploaded by

TALHA JAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Understanding Classes in OOP

Uploaded by

TALHA JAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

OBJECT ORIENTED Lecture 3

PROGRAMMING
Prepared by: Mian Saeed Akbar
Classes
o Classes forms the basis for Object Oriented Programming
o It is used to define the nature of an object class Time {
private:
o It enable the programmer to model objects int hour;
int minute;
 Attributes (data members) int second;
 Behaviors (member functions)
public:
o Defined using keyword class Time();
void setTime( int, int, int );
o Member functions
}; // end class Time
 Member functions are also called Methods
 Invoked in response to messages sent to an object
o Once a class is defined, we can use its name to declare objects of
that class
o Objects like those of a structure can be like ordinary variables,
arrays, pointers or reference of an object
Member Access Specifiers
o A class can contain three types of member access specifier
o These are public, private and protected
o These are the commands that specify whether a member of a class
can be access from outside the class or not
1. private:
 Members of a class which can be assess from within the class
 These members can not be accessed from outside the class
 Normally data members are declared as private
 Member functions can also be declared as private, but these
member functions will be accessible only from inside the class
 Utility or helper functions are also kept private
 All members that come after the specifier private and up to the
next specifier are declared as private
Member Access Specifiers
 The default member access specifier in a class is private, while in
structure it is public
 Accessing data from within the class only is called data hiding
and hence all the members declared as private are hidden from
outside the class

2. public:
 Public members of a class can be access from both inside and
outside of the class
 Normally member functions are declared as member public, data
members can also be declared as public
 Public member functions implements the behaviors or services
the class provides to its clients normally refers as class interface
or public interface
Member Access Specifiers
3. protected:
 Protected specifier is used in inheritance
 In protected specifier, the private member of the parent class can
be access by the children but can not be access form outside the
class
1 class Time {
2
3 public: Function prototypes for
4 Time(); Definition of class
Class// begins
body with left member functions.
startspublic
constructor
5 withint,
void setTime( int, keyword class.
intbrace.
); // set hour, minute, second
6 void printUniversal(); Member access specifiers.
// print universal-time format
7 void printStandard(); Constructor has same
// print name as format
standard-time
8 class, private
Time, anddata
no return
members
9 private: type. accessible only to member
10 int hour; // 0 - 23 (24-hour clock format)
functions.
11 int minute; Class
0 -body
59 ends
//Definition terminatesright
with with
12 int second; brace.
// 0 - 59
semicolon.
13
14 }; // end class Time
Class
 Data members of class can not be initialized where they are declared in
the class body
 These data members should be initialized by the class constructor, or
they can be assigned value by set functions
 The class definition contains declaration of the class data members and
the class members function
 The member function declaration are the function prototypes
 Member functions can be defined inside a class, but we can also define
the functions outside the class definition
 Member functions outside the class can be define with scope resolution
operator preceded by the class name
void Time::setTime(int h, int m, int s){…}
Class
 If a member function is defined in a class definition, the member function
is automatically inlined
 The member function defined outside the class may be defined inline
explicitly using the keyword inline
Constructor Function
 When a class object is created, its members can be initialized by that
class constructor function
 It is a special member function with the same name as the class and
has no return type
 It called when object instantiated
 The programmer provides the constructor, which is invoked
automatically each time an object of that class is created
 If the programmer do not write constructor, the compiler provides the
constructor to the class
 Constructor can be overloaded to provide a variety of means for
initializing the object of a class
 Data members of class can be either initialized in a constructor of the
class or their values may be set later after the object is created
Constructor Function
 Constructors can contain default arguments, to initialize the object to a
consistent state
 A programmer supplied constructor that default all its arguments or
explicitly require no argument is also a default constructor i.e. a
constructor that can be invoked with no arguments
 There can be only one constructor per class
Time(int h, int m, int s){
hour = h;
month = m;
second = s;
}
Time(){
hour = 09;
minute = 00;
second = 00;
}
Destructors
 A special member function
 It has the same name as class preceded with tilde (~) sign
 It has no arguments and no return type
 It cannot be overloaded
 A class may have only one destructor
 When there is nor explicit destructor, the compiler creates an empty
destructor
 A destructor is called when an object is destroyed i.e. for automatic
objects when the object leaves the scope in which it is defined
 It performs “termination housekeeping”, the work to be done just
before an object is to be destroyed
Time::~Time(){
cout<<"Object is being deleted"<<endl;
}

You might also like