Lab Manual Week 3 Arid
Lab Manual Week 3 Arid
Rawalpindi
UIIT
A class is like a blueprint of data member and functions and object is an instance of class.
For example, let’s say we have a class Car which has data members (variables) such as
speed, weight, price and functions such as gearChange(), slowDown(), brake() etc. Now let’s
say I create a object of this class named FordFigo which uses these data members and
functions and give them its own values. Similarly, we can create as many objects as we want
using the blueprint(class)
class Car
//Data members
char name[20];
int speed;
int weight;
public:
//Functions
void brake(){
void slowDown(){
};
int main()
//ford is an object
Car ford;
The body of the class contains two unfamiliar keywords: private and public. A key feature of
OOP is data hiding; means that data is concealed within a class, so that it cannot be accessed
mistakenly by functions outside the class. The primary mechanism of hiding data is to put it in a
class and make it private. Private data or functions can only be accessed from within the class.
Public data or functions, on the other hand, are accessible from outside the class.
Usually the data within a class is private and the functions are public. This is a result of how
classes are used. The data is hidden so it will be safe from accidental manipulation, while
functions that operate on the data are public so they can be accessed from outside the class.
However, 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.
Constructor
A class constructor is a special member function of a class that is executed whenever we create
new objects of that class.
A constructor will have exact same name as the class and it does not have any return type at all,
not even void. Constructors can be very useful for setting initial values for certain member
variables.
Example:
Header Code:
Main Code:
Source Code:
Lab Task(s)
1. Write a C++ program that creates a class called laptop. The data members of the
class are:
1. brand (string)
2. model(string)
3. serial (int)
4. color (string)
5. price (float)
6. processor speed (float)
7. RAM (int)
8. screen size(float).
The getter & setter should accept the laptop brand, model, serial, color, price,
processor speed, ram and screen size.
Make a member function name display to display the value of all attributes.
Make 3 objects of laptop and display its values.
2. Write down a C++ Program that creates a class called Student. The data members
of the class are:
1. Name (String)
2. Roll No (Integer)
3. TotalMarks (Integer)
4. Percentage (Double)
The getter & setter should accept the student name, rollno, totalmarks , percentage.
Make a member function name display to display the value of all attributes.
Make 3 objects of laptop and display its values.