Week2_Lecture
Week2_Lecture
Course:
© www.uogsialkot.edu.pk
Structures and Classes
•Classes and structures are closely related.
• Example:
• Student is a class
• Ahmad is a student hence an object of student
class
• Fruit is a class
• Apple is an object
double magnitude;
char scale;
Defining a class
• Similar to a structure
class class_name
{
//class definition
};
class Temperature
{
double magnitude;
char scale;
};
Temperature temp1;
temp1 magnitude
scale
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.
class Temperature
{
public:
void display();
private:
double magnitude;
char scale;
};
Object.Function();
temp1.display();
References
• Robert Lafore, Chapter 6: Objects and
Classes
• C++, How to Program by Deitel and
Deitel