Sunteți pe pagina 1din 10

CLASSES AND OBJECTS

QUIZ NO 1, SEC-B
• Take out a piece of paper
• Write down your Name, Reg no and
Section
• Ready?
Quiz No. 1
• Define and list down the
advantages/features of Encapsulation,
inheritance and Polymorphism.
• Time: 5 min
Classes and Objects
• A CLASS is an expanded concept of a data structure:
instead of holding only data, it can hold both data and
function.
-The function components of a class are called as member
functions or methods.
-The data components as attributes or data members
• An OBJECT is an instance of a class. In terms of
variables, a class would be the type, and an object would
be the variable. Means an object has the same
relationship to a class that a variable has to a data type.
• Let us have a look at a simple program, which employs
class and objects
• Prog1.cpp
• Classes are generally declared using the keyword class,
with the following format:
class class_name
{
access_specifier_1:
member1;
access_specifier_2:
member2; ...
};
Class_name object_names
• Where class_name is a valid identifier for the class,
object_names is list of names for objects of this class.
The body of the declaration can contain members, that
can be either data or function declarations, and
optionally access specifiers.
• All is very similar to the declaration on data structures,
except that we can now include also functions and
members, but also this new thing called access specifier.
Access Specifiers
• An access specifier is one of the following three keywords: private,
public or protected. The data hiding feature of OOP is
implemented by these 3 keywords. These specifiers modify the
access rights that the members following them acquire:
• Private members of a class are accessible only from within other
members of the same class or from their friends.
• Protected members are accessible from members of their same
class and from their friends, but also from members of their derived
classes.
• Finally, Public members are accessible from anywhere where the
object is visible.
• Usually the data within a class is private and the functions are
public. The data is hidden so it will be safe from accidental
manipulation while functions that operate on the data are public so
that they can be accessed from outside the class. However there is
no rule that data must be private and functions public.
Prog1.cpp
• Declaring the class: Starts with a keyword class, its body is delimited
by braces and terminated by a semicolon
• Data hiding: Don’t confuse data hiding with security techniques used
to protect data bases. It just means hiding data from parts of the
program that don’t need to access it. More specifically one class’s
data is hidden from the other classes
• Data members and function members: In prog1.cpp we have 1 data
member and 2 function members.
• Member functions declared within the class: The 2 member
functions and declared and defined inside the class. (‘setdata’
accepts a value as a parameter and sets the ‘data’ to some variable
while the ‘showdata’ functions displays the value stored in ‘data’.)
• Using the class: Now that the class is declared we have to use it,
using the ‘main’ function. We will define objects and then access the
member functions.
• Defining the objects: The first statement in main()
secb o1, o2; defines two objects o1 and o2 of class named ‘secb’.
Defining an object is similar to defining a variable of an data type.
Defining objects means creating them or instantiating them. The
term instantiating arises because an instance of the class is created.
Objects are also called ‘instance variables’.
Prog1.cpp
• Calling member functions: The next two statements in main( ) call the
member function setdata( ).
o1.setdata(25);
o2.setdata(50);
• This syntax is used to call a member function that is associated with a
specific object. Here the dot operator (also called ‘the period’ or ‘class
member access operator’.) connects the object name and the member
function.
• o1.setdata(25); statement executes the setdata( ); member function. The
function (as defined) sets the ‘data’ to the value 25 in the object o1.
Also o2.setdata(50); statement executes the setdata( ); member function.
The function (as defined) sets the ‘data’ to the value 50 in the object o2.
• Similarly two calls to the showdata( ) functions will cause the two objects to
display their values.
o1.showdata();
o2.showdata();
• The statement o1.showdata( ); can be thought of as sending a message to
o1 telling it to show its data. The same goes for the other statement.
Prog2.cpp
• Now write the same program that accepts
values from the user and displays those
values!
Prog3.cpp
• Now write a program of class named ‘sumsub’
such that it will take two values from the user,
adds and subtracts them and gives the user its
result and displays it.
• Then replace the cin commands with such
statements that the function accepts values as
parameters, displays them, adds and subtracts
them and shows user the result.

S-ar putea să vă placă și