Lab Sheet 3
Concept of Objects and Classes
Objects
An object consists of the data and the functions that operate on the data. Object data is usually private: no functions other than those within the same object can access it. On the other hand, the functions within the object are usually public. They can be accessed by the other functions in the program, including the functions in the other objects. So the function in one object can access the data in the other object through the function of that object.
Classes
In object-oriented programming, objects are variables of classes. It is a specification for any number of objects based on that class. A class itself is a template (data type) for objects. When we actually define the object of a class, we are requesting to allocate memory for that object to hold its data. Classes are actually user-defined data types.
Access Specifiers
Access specifiers define the accessibility of the members of a class. C++ provides three access specifiers: private, public, and protected. The two most commonly used are private and public.
Members declared as private can only be accessed from within the class itself. They cannot be accessed directly from outside the class or from objects of the class. This is the default access level in a class, meaning that if no access specifier is mentioned, the members are treated as private. Data members are usually kept private to prevent accidental modification from outside the class. This is the principle of data hiding.
Members declared as public can be accessed from anywhere in the program, including from outside the class and from objects of the class. Member functions are usually kept public so that they can be called from the main program to interact with the private data.
For example:
class Demo
{
private:
int rollno; // accessible only within the class
float score; // accessible only within the class
public:
void setdata(int rl, float sc){ // accessible from outside
rollno = rl;
score = sc;
}
void showdata(){ // accessible from outside
cout << "Roll No: " << rollno << " has scored " << score << endl;
}
};
In the above class, rollno and score are private data members. They cannot be accessed directly using an object, as
d1.rollno = 12; //error: private data
would cause a compiler error. Instead, they are accessed indirectly through the public member functions setdata() and showdata().
Data Member
The data items within a class are called data members. There can be any number of data members in a class. Normally, data members follow the keyword private, so they can be accessed from within the class but not from outside. That is why we say that OOP has the feature of data hiding. So, it is safe from accidental alteration. Let's see a class example.
class Demo
{
private:
int rollno; //data member
float score; //data member
public:
void setdata(int rl, float sc){ //member function
rollno=rl; score=sc;
}
void setdata( ){ //overloaded version of setdata()
cout<<"\nEnter the roll no: ";
cin>>rollno;
cout<<"\nScore: ";
cin>>score;
}
void showdata( ){ //member function
cout<<"\nRoll No: " <<rollno<<"has scored "<<score<<endl;
}
};
The above class contains two data items: rollno and score. Here rollno is of type int, and score is of type float.
Member Function
Member functions are functions that are included within a class. In the above example, there are two member functions in the class demo: setdata() and showdata(). Each function can have one or more statements. The functions setdata() and showdata() follow the keyword public, which means that they can be accessed from outside the class. It is also possible to declare functions within a class and define them elsewhere.
The class demo can be used as follows
int main()
{
Demo d1,d2;
d1.setdata(12,10.4);
d2.setdata(); //calls overloaded version of setdata()
d1.showdata();
d2.showdata();
return 0;
}
Declaring Objects
In the above example, d1 and d3 are defined as objects of the class Demo. Declaring the object is similar to declaring a variable of a basic data type. Declaring the object causes space to be reserved for them, i.e., an instance of the class is created. In general, objects in the program represent physical objects: things that can be felt or seen, for example, a circle, person, car, etc.
Similar to array declaration of standard data types, an array of objects can also be declared as:
Demo obArr[5];
This statement declares an array of type Demo with 5 elements.
Calling Member Functions
Member functions are called as follows
d1.setdata(12,10.4);
d3.setdata();
d1.showdata();
d3.showdata();
This syntax is used to call a member function that is associated with a specific object. Since these functions belong to the class demo, they must always be called in connection with objects of this class. Here, the first member function is called by passing the values, while the second is called without passing any value.
Exercises
Write a simple program that converts the temperature on the Celsius scale to the Fahrenheit scale and vice versa and returns the value using the basic concept of class and object. Make separate classes for Celsius and Fahrenheit, which will have private members that hold the temperature value, and make conversion functions in each class for conversion from one to another. For example, you need to have a function toFahrenheit() in class Celsius that converts to the Fahrenheit scale and returns the value.
Assume that you want to check whether the number is prime or not. Write a program that asks for numbers repeatedly. When it finishes checking a number, the program asks if the user wants to do another calculation. The response can be 'y' or 'n'. Don't forget to use the object-oriented concept.
Create a class called carpark that has an int data member for the car ID, an int data member for charge/hour, and a float data member for the parked time. Make separate functions to set data members, show data members, show the parked hours, and the total charge of the corresponding car ID. Member functions should be called from other functions or the main() function.
Write a program with classes to represent a circle, a rectangle, and a triangle. Each class should have data members to represent the actual objects and member functions to read and display objects, find the perimeter and area of the objects, a function to check if two shapes have equal area, and other useful functions if needed. Use the classes to create objects in your program.
Write a program with a class called Student that has private data members for roll number, name, and marks in three subjects. Include member functions to set and display the data. Add a member function to calculate and return the total and average marks. Display the grade based on the average: Distinction (80 and above), First Division (60–79), Second Division (45–59), Third Division (32-44), and Fail (below 32). Also in your program, create an array of students and find the average and standard deviation of the total marks of the students.
Write a program with a class called Time that has private data members for hours, minutes, and seconds. Include member functions to set the time, display the time in the format HH:MM:SS, and add two Time objects together. Also put an option to display time in AM/PM format and 24-hour format.