0% found this document useful (0 votes)
21 views

Week2_Lecture

This is informative lecture .. uh should take ot for gaining your further more knowledge and seeking many different things to achieve your goals in this hardship worlds.. Thank you!

Uploaded by

maryammaryam3467
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Week2_Lecture

This is informative lecture .. uh should take ot for gaining your further more knowledge and seeking many different things to achieve your goals in this hardship worlds.. Thank you!

Uploaded by

maryammaryam3467
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Course:

Course:

Object Oriented Programming


3.00 Credit Hours, Spring 2022,
3.00 Credit Hours,
Undergraduate
Undergraduate Program
Program
Instructor: Ms. Wajeha
Instructor: WajehFareed
Slide Credits: Dr. Sabeen
SESSION 1, 2 Javaid
Week 2

© www.uogsialkot.edu.pk
Structures and Classes
•Classes and structures are closely related.

•A structure is a collection of simple data variables that


are grouped together and named under single entity.

•A class is a group of data variables and functions.


These data variables and functions are called class
members.
• The variables declared within a class declaration
are called class data members.
• The functions declared within a class declaration
are called member functions/methods. These
methods manipulate the data members.
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 , current speed) and behavior
(changing gear, changing pedal cadence,
applying brakes).
Cont…
• For each object that you see, ask yourself two
questions:
• "What possible states can this object be in?"
and
• "What possible behavior can this object
perform?“

• Real-world objects vary in complexity

• You may also notice that some objects, in turn,


will also contain other objects.

• These real-world observations all translate into


the world of object-oriented programming.
Software Object
• Software objects are conceptually similar
to real-world objects: they too consist of
state and related behavior. An object
stores its state in
• Data members
• and exposes its behavior
through methods
• Methods operate on an object's internal
state and serve as the primary
mechanism for object-to-object
communication.
Class vs. Objects
• An object has the same relationship to a class as a
variable has to a data type

• An object is said to be an instance of a class

• As you don’t assign values to types rather to


variables
int = 10; //wrong
int y = 10; //right

• Similarly an object of a class has to be created


before it can be assigned any values
rectangle rect1;
rect1.width = 1.5;
Class vs. Objects
• Class is a blue print of an object, which is non-live entity.

• Object is instance of class, which is a live entity.

• Example:
• Student is a class
• Ahmad is a student hence an object of student
class
• Fruit is a class
• Apple is an object

• A class is not a living entity, it is just a engineering design


that how an object of this class look like.

• Object are living entities


Problem Statement
Write a program that reads a temperature
(either Fahrenheit or Celsius), and displays that
same temperature in both scales
Preliminary Analysis
A temperature has two attributes:
• its magnitude (a double), and
• its scale (a character)
Building a class
Begin by defining variables to store the attributes of
the object being represented i.e. a temperature

double magnitude;
char scale;
Defining a class
• Similar to a structure

• Instead of struct keyword, class keyword is used

class class_name
{
//class definition
};

class Temperature
{
double magnitude;
char scale;
};

• Like a structure, the body of the class is delimited by braces and


terminated by a semicolon.
• Declaring this class does not allocate memory for a class.
• It just tells the compiler what Temperature is and what are its members.
Defining an object
• An object of a class is defined as follows

Temperature temp1;

• and object temp1 can be visualized as follows:

temp1 magnitude

scale

• The data members magnitude and scale within


temp1 are uninitialized.
Information hiding
• Information hiding means that information is
concealed within a class so that is cannot be
accessed mistakenly by functions outside the class.
• The primary mechanism for information hiding is
to put it in a class and make it private.
• Classes have a public section and a private section.
• Private data or functions can only be accessed
from within the class.
• Public data or functions are accessible from
outside the class.
• The default visibility of all the members of a class
is private.
Cont…
Cont…
• Normally, data in a class is kept private i.e. not accessible
to outside the class. In case, we make the data public, it is
same as structure and can be accessed outside the class.

• Methods are kept public i.e. can be accessed outside the


class. Methods can access and manipulate the private
data members of the class.

class Temperature
{
private:
double magnitude;
char scale;
public:
//public data and functions
};
Cont…
• What we want to show others will be in Public
section hence it becomes interface of the
class. With the help of public interface, the
objects can be manipulated.

• What we do not want to show will be in


Private section hence it becomes the inside of
the class i.e. implementation

• By hiding implementation details from others,


we separate the interface from
implementation.
Member Functions - Prototype
• Member functions are functions that are included within a
class.

• To display the temperature, we declare a function display()

class Temperature
{
public:
void display();
private:
double magnitude;
char scale;
};

• By declaring the prototype within the class, we tell the


compiler that class Temperature has a function member
named display()
Member Function - Definition
void Temperature::display()
{
cout << “Magnitude:” <<magnitude ;
cout <<“ ,Scale:” << scale;
}

• Since the function returns nothing, so its return-


type is void
• The full name Temperature::display() tells the
compiler this is a Temperature member function i.e.
• The double colon is called scope resolution
operator i.e. it resolves the scope and tells the
compiler that the function display() belongs to
whom.
Member Function - Calling
• Operations on a class object are usually
implemented as class member functions.

• A call to a member function:

Object.Function();
temp1.display();
References
• Robert Lafore, Chapter 6: Objects and
Classes
• C++, How to Program by Deitel and
Deitel

You might also like