Lab Sheet 3
Lab Sheet 3
Concept of Objects and Classes
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.
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.
Comments in C++
In C++, a new symbol for comment '//' (double slash) is introduced and is called a single-line comment. The content after // until the end of the line is treated as a comment.
C comment symbols '/*' and '*/' are also valid and can be used for multi-line comments. This commenting style is called a multiline comment
cause the string "This is the first lab in C++" to be displayed on the screen. Here cout is the predefined object that represents the standard output stream in C++.
The operator '<<', insertion operator, has a simple interface, i.e., it is not necessary to specify the data type of the variable on its right. The insertion operator automatically works with any type of variable. Another advantage of this operator is that user-defined data types can also be used with this operator, which is not possible with printf( ) function.
The following statement reads the value from the keyboard and places it in variable size.
The cin identifier represents the standard input device. The >> operator (extraction operator) takes the input from the device. The function is smart enough to interpret the input according to the data type of the variable that holds the value. If a variable size is an integer and the user types "25", the integer value 25 will be placed in size. If the variable size is a string, the string "25" will be placed in it.
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.
The above class contains two data items: rollno and score. Here rollno is of type int and score is of type float.
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 it elsewhere.
The class demo can be used as follows
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 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 programs represent physical objects: things that can be felt or seen, for example, a circle, person, car, etc.
Calling Member Functions
Member functions are called as follows
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.
Write a simple program that converts the temperature in Celsius scale to Fahrenheit scale and vice versa using the basic concept of class and object. Make separate classes for Celsius and Fahrenheit which will have the 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 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 int data member for car id, int data member for charge/hour, and float data member for the parked time. Make functions to set data members and show the charges and parked hours of the corresponding car id. Make functions for setting and showing the data members. Member function should be called from other functions.
Write a program with classes to represent a circle, rectangle, and triangle. Each class should have data members to represent the actual objects and member functions to read and display objects, find perimeter and area of the objects, and other useful functions. Use the classes to create objects in your program.
Assume that an object represents an employee report that contains information like employee id, total bonus, total overtime in a particular year. Use an array of objects to represent n employees' reports. Write a program that displays the report. Use setpara() member function to set report attributes by passing the arguments and member function displayreport() to show the report according to the parameter passed. Display the report in the following format.