0% found this document useful (0 votes)
28 views26 pages

Oop Chap 2

Chao
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)
28 views26 pages

Oop Chap 2

Chao
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/ 26

CHAPTER 2

Classes
Classes

Classes- Class can be defined as a plan of the


object.

It is basically a collection of objects which work as


building blocks.
Generally a class specification has two parts
a) Class declaration
b) Class function definition

the class declaration describes the type and scope of its members.

The class function definition describes how the class functions are implemented.

Syntax:-

class class-name

private:

variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
The members that have been declared as private can be accessed only from with in the class.
On the other hand , public members can be accessed from outside the class also.
The data hiding is the key feature of oops.
The use of keywords private is optional by default, the members of a class are private.
The variables declared inside the class are known as data members and the functions are known
as members functions.
Only the member functions can have access to the private data members and private functions.
However, the public members can be accessed from the outside the class.
The binding of data and functions together into a single class type variable is referred to as
encapsulation.
Syntax:-
class item
{
int member;
float cost;
public:
void getdata (int a ,float b);
void putdata (void);
The class item contains two data members and two function members,
the data members are private by default while both the functions are
public by declaration.
The function getdata() can be used to assign values to the member
variables member and cost, and putdata() for displaying their values .
These functions provide the only access to the data members from
outside the class.
Simple Program For Declaring Classes
#include <iostream>
using namespace std;
class smalljob //define a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{
somedata = d;
}
void showdata() //member function to display data
{
cout << "Data is = "<< somedata << endl;
}
};
int main()
{
smalljob s1, s2; //define two objects of class smallobj
s1.setdata(1066); //call member function to set data
s2.setdata(1776);
s1.showdata(); //call member function to display data
s2.showdata();
return 0;
}

The O/P
The Data Is = 1066
The Data Is = 1776
The class smalljob defined in this program contains one data item and two member functions.
The two member functions provide the only access to the data item from outside the class.
The first member function sets the data item to a value, and the second displays the value.
Placing data and functions together into a single entity is a central idea in object-oriented
programming.
This is shown in Figure 6.1
Classes and Objects
An object has the same relationship to a class that a variable has to a data type.
An object is said to be an example of a class,
In smalljob , the class—whose name is smalljob —is defined in
the first part of the program.
Later, in main(), we define two objects—s1 and s2—that are
example of that class.
Each of the two objects is given a value, and each displays its value.
Here’s the output of the program:
Data is 1066 ← object s1 displayed this
Data is 1776 ← object s2 displayed this
We’ll begin by looking in detail at the first part of the program—the
definition of the class smalljob .
Later we’ll focus on what main() does with objects of this class
Defining the Class
Here’s the definition (sometimes called a specifier) for the class smalljob , copied from the
SMALLJOB listing:

class smalljob //define a class

private:

int somedata; //class data

public:

void setdata(int d) //member function to set data


{
somedata = d;
}
void showdata() //member function to display data
{
cout << “\n Data is “ << somedata;
}
};
Defining the Class (cont)
The definition starts with the keyword class, followed by the class name—smalljob
in this

example.

Like a structure, the body of the class is delimited by braces and terminated by a
semicolon.

(Don’t forget the semicolon.

Remember that data structures such as structs and classes end with a semicolon,
while control structures such as functions and loops do not end with a semicolon.)
Private And Public
The body of the class contains two unfamiliar keywords: private and public.

What is their purpose?

A key feature of object-oriented programming is data hiding.

This term does not refer to the activities of particularly paranoid programmers; rather it means
that data is concealed within a class so that it cannot be accessed mistakenly by functions
outside the class.

The primary mechanism for 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.
Private And Public(cont)
Class Data

The smalljob class contains one data item: somedata, which is of type int.

The data items within a class are called data members (or sometimes member
data).

There can be any number of data members in a class, just as there can be any
number of data items in a structure.

The data member somedata follows the keyword private, so it can be accessed
from within the class, but not from outside.
Member Functions
Member functions are functions that are included within a class.

(In some object-oriented languages, such as Smalltalk, member functions are called
methods; some writers use this term in C++ as well).

There are two member functions in smalljob: setdata() and showdata().

The function bodies of these functions have been written on the same line as the
braces that

delimit them.

You could also use the more traditional format for these function definitions:
void setdata(int d)
{
somedata = d;
}
void showdata()
{
cout << “\n Data is “ << somedata;
}
However, when member functions are small, it is common to compress
their definitions this way to save space.
Because setdata() and showdata() follow the keyword public, they can
be accessed from outside the class.
We’ll see how this is done in a moment.
Figure 6.3 shows the syntax of a class definition.
Functions Are Public, Data Is Private
Usually the data within a class is private and the functions are public. This is a

result of the way classes are used.

The data is hidden so it will be safe from accidental manipulation, while the

functions that operate on the data are public so they can be accessed from outside

the class.

However, there is no rule that says data must be private and functions public; in

sometime you may find you’ll need to use private functions and public data.
Simple Program For Book
#include <iostream>

using namespace std;

class Book

public:

string title;

string author;

int price;

int pages;

};
int main()
{
Book book1;
book1.title = "C++ LEVEL 1";
book1.author = "ALI";
book1.price = 160;
book1.pages = 17;
cout << "Book1" << endl;
cout << "Book1 title " << book1.title <<"\n" << endl;
cout << "Book1 author " << book1.author <<"\n"<< endl;
cout << "Book1 price " << book1.price <<"\n"<< endl;
cout << "Book1 pages " << book1.pages<<"\n" << endl;
return 0;
}
The O/P Is
Book1
Book1 title C++ LEVEL 1

Book1 author ALI

Book1 price 160

Book1 pages 17

You might also like