[go: up one dir, main page]

0% found this document useful (0 votes)
68 views6 pages

Lab Manual Week 3 Arid

The document discusses classes and constructors in C++, explaining that a class acts as a blueprint for objects with data members and functions, and that a constructor is a special member function that initializes objects when they are created; it also provides examples of defining a Car class with data members and functions and creating objects from it, and explains access specifiers like public and private.

Uploaded by

Rana Farzan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views6 pages

Lab Manual Week 3 Arid

The document discusses classes and constructors in C++, explaining that a class acts as a blueprint for objects with data members and functions, and that a constructor is a special member function that initializes objects when they are created; it also provides examples of defining a Car class with data members and functions and creating objects from it, and explains access specifiers like public and private.

Uploaded by

Rana Farzan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PMAS Arid Agriculture University

Rawalpindi
UIIT

Class: Spring-2021 Subject: OOP


Course Code: CS443 Class/Lab Muhammad Ahsan Arshad
Instructor:

-------------------- LAB 03 --------------------


Classes and Constructor
Learning Objective:
1. Classes and Object Creation
2. Access specifies. How to use it?
3. What is Constructor.
4. Practical walk through.
5. Exercises

Classes and Objects

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;

Access specifies (Private, Public and protected)

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.

You might also like