0% found this document useful (0 votes)
15 views

1.lab 1 - Classes

Uploaded by

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

1.lab 1 - Classes

Uploaded by

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

EE-203 DS&A Lab

Experiment 1:
“Implement Classes & Objects using C++”

Batch 8 Fall 2020


Outline

 What is a class in C++


 Defining a class using C++
 Members of a class
 Data Members
 Member functions
 Member Access Specifiers
 What is an object in C++
 Declaring Objects of a class
 Calling member functions through classes
 Defining member functions outside the class
 Storage of objects in memory
What is a class in C++
 A class is a collection of data and functions
 Classes are defined to create user defined data types
 Classes are similar to structures
 Structures are user defined datatypes that hold only data
 Classes are user defined data types that hold data & functions to use
that data

Defining a class in C++

A class in Object Oriented Programming (OOP) is a collection of


data & functions. It is a user defined data type in OOP
Members of a class
 A class contains data items and functions known as members of
a class.
 Data items of a class are called data members

 Functions of a class that are defined to work on data members


are called member functions
Member Access Specifiers
 Commands that determine whether a member of a class can be
accessed from outside the class or not are called member access
specifiers.
 Private
 Public
 Private Specifier:
 The members of a class that can be accessed only from within the
class are called private members.
 They can not be accessed from outside the class
 Private members can only be used by the members of the same class.
 They can only be accessed from within the class
 Default mode is Private
 Public Specifier:
 Public members of a class can be accessed from inside and outside
the class.
 Member functions are usually public
 Data members can be declared as public
What is an object in C++
 The variables declared of the class (user-defined data type in OOP)
are known as objects.
 A class has data members and member functions thus the objects
declared with that class will consist of the data members and
member functions defined in that class.
Lab Tasks
 Lab Task: Write a C++ program by using a class to input two values from
the user using a member function of a class. Display the sum of the two
values by using another member function of the class.

 Lab Task: Write a C++ program by using a class to input the name of
the student and marks of three subjects. Calculate the total marks and
average marks. Each subject has maximum marks of 100.
Defining Member functions Outside
the Class
 Member functions of a class can be defined outside the class.
 In such case only prototype of the member function is declared inside the
class.
 Scope resolution operator is used in the member function declaratory to
define the function of class outside the class

You might also like