0% found this document useful (0 votes)
9 views22 pages

Lec 02 Class, Objects and Access Specifiers (1)

The document provides an overview of Object Oriented Programming (OOP) concepts, focusing on classes and objects, including their definitions, characteristics, and how to declare and access class members in C++. It explains the role of access specifiers (public, private, protected) and illustrates these concepts with examples, particularly in C++. Additionally, it discusses the initialization of class data members and the instantiation of objects.

Uploaded by

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

Lec 02 Class, Objects and Access Specifiers (1)

The document provides an overview of Object Oriented Programming (OOP) concepts, focusing on classes and objects, including their definitions, characteristics, and how to declare and access class members in C++. It explains the role of access specifiers (public, private, protected) and illustrates these concepts with examples, particularly in C++. Additionally, it discusses the initialization of class data members and the instantiation of objects.

Uploaded by

f2024266821
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Object Oriented Programming

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

• The data items within a class smallobj


somedata are called data members (or
sometimes member data).
• There can be any number of data
members in a class, just as there can be
any number of data items in a structure.
FUNCTIONS ARE PUBLIC, DATA
IS PRIVATE
• Usually the data within a class is private
and the functions are public.
• There is no rule that data must be private
and functions public; in some
circumstances you may find you’ll need to
use private functions and public data.
INITIALIZATION
 Class data member cannot be initialized at
declaration time.
 By default all data members have garbage
value.
 If all members are declared public then, they
can be initialized at object declaration time.
 Similar to structure initialization time.
 Otherwise we require a constructor for this.
DEFINING OBJECTS
 Defining an object is similar to defining a
variable of any data type: Space is set aside
for it in memory.
 This is also called instantiating them. The
term
instantiating arises because an instance of
the
class is created.
 An object is an instance (that is, a specific
example) of a class. Objects are sometimes
called instance variables
References
• Robert Lafore, Chapter 6: Objects and
Classes
• C++, How to Program by Deitel and Deitel

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy