Objects and Classes_slides (1)
Objects and Classes_slides (1)
Session Objectives
Upon completing this session you should
be able to:
Declare a class
Differentiate between member variables and member
functions
Define member functions
Create objects from a class
Access data members
Introduction to Classes
Object
At the heart of object-oriented programming is the object. An object is
an entity -an instance of a data type -that has structure and state. Each
object defines operations that may access or manipulate that state.
Class
A class is a user defined data type, which holds its own data members
and member functions, which can be accessed and used by creating an
instance of that class. A class is like a blueprint for an object.
Classes are fundamental building blocks of object-oriented programs. A
class binds data and functions together. The data and functions of a
class are its members.
Class Example:
Consider the Class of Cars. There may be many cars with different
names and brand but all of them will share some common properties,
e.g., all of them will have 4 wheels, Speed Limit, Mileage range etc. So
here, Car is the class and wheels, speed limits, mileage are their
properties (attributes).
A Class is a user defined data-type which has data members and
member functions.
Data members are the data variables
Member functions are the functions used to manipulate these variables.
These data members and member functions together define the
properties and behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit,
mileage etc and member functions can be apply brakes, increase speed
etc
Base class v derived class
A base class is a class, from which other classes are derived. It
facilitates the creation of other classes that can reuse the code implicitly
inherited from the base class (except constructors and destructors). A
base class may also be called parent class or superclass
Derived Class: A class that is created from an existing class. It inherits
all members and member functions of a base class. The derived class
can have more functionality with respect to the Base class and can
easily access the Base class. A Derived class is also called a child class
or subclass. Defining Class and Declaring Objects
A class is defined in C++ using keyword class followed by the name of
class. The body of class is defined inside the curly brackets and
terminated by a semicolon at the end.
Declaring a class
Specifying the data members and member functions of a class is known as
declaring a class.
The class definition begins with class keyword followed by the name of the class.
The name of a class is an identifier and it is not supposed to be a reserved word.
The body of a class is enclosed within the opening curly brace ({) and closing curly
brace (}). The closing curly brace is terminated by a semi-colon (;). The body of a
class consists of data members and member functions.
The syntax for defining a class:
Class class_name
{
private:
data_type number1;
data_type number2;
………….
data_type numbern;
public:
Member f u n c t i o n 1 ;
member f u n c t i o n 2 ;
……………………..
};
Class Syntax
Example
Class Rectangle{
private:
int length;
int width;
public:
void print_dimension(void);
};
Declaring Objects:
When a class is defined, only the specification for the object is defined;
no memory or storage is allocated. To use the data and access
functions defined in the class, you need to create objects.
Syntax:
ClassName ObjectName;
Defining Member Functions
A class in C++ consists of its members. These members can be either data or
functions. The functions are called member functions. Each instance of a class is
an object. Each object contains the data components specified in the class
(unless the data components are static). A member function is used to act on an
object. Member functions are also called methods.
The data members and member functions details are controlled using private,
public and protected keywords. Private, public and protected keywords are also
referred to as access specifiers/modifiers and are always followed by a colon (:).
Private section of a class has data members and member functions
that are available only within the class (except by friend functions).
Public section makes the data members and member functions within
it to be available to all class members and to the other classes’
members and applications.
Protected section has data members and member functions that are
available to the class itself and any other derived class.
The beginning of each section is marked by keywords private: public:
and protected:
NB: By default data members and member functions are private.
Normally the data members are put in the private section of a class and
the member functions are put in the public section.
Ways of defining member functions :
Member functions can be defined within a class definition. Still consider the
print_dimension() function declared in the class Rectangle above.
This function can be defined in the class as shown below.
class Rectangle
{
private:
int length;
int width;
public:
void funt()
)
{
cout<< “The Length is:”<length << “cm\n” << “The width is :” <<width < “cm\n”<<endl;
}
};
Creating objects from a class
a) Form 1
class-name variable-name; e.g.
Rectangle rect1;
b) Form 2
class-name variable-name(arguments); e.g.
Rectangle rect2(4, 5);
The above two statements create class variables rect1 and rect2 of type
Rectangle.
A class variable is an object.
In that case rect1and rect2 are objects of type Rectangle
Accessing class members
All private data members are accessible only by class member functions
(this restriction does not apply to friend functions).
It is illegal to have a statement for example within the main method
that accesses the value stored in data member.
External functions access private data members by calling public
members of a class. The syntax for doing this is object-name. function-
name( arguments);
This is what is referred to as message passing. The receiver of the
message is object-name. The name of the message is function-name.
The information that is passed to the object in order for the object to
respond to the message is “arguments” E.g. rect1.print_dimension();
Below is an example of a C++ program that is made of
a class
/* Rectangle.cpp. This program demonstrates the use classes in a C++ program.*/
#include<iostream>
using namespace std;
class Rectangle{
private: int length; int width; public:
Rectangle(int l, int w);
void print_dimension(void);
};
int main(void)
{
Rectangle rect1(5,3); rect1.print_dimension();
return 0;
}
/* Definition of Rectangle member function*/
Rectangle :: Rectangle(int l, int w)
{
length=l; width=w;
}
/*
Definition of print_dimension member function*/
void Rectangle :: print_dimension(void)
{
cout<<”The Length is: “<<length <<”cm\n” <<”The width is : “<<width <<”cm\n”
<<endl;
}
If you compile and execute this program, the output is The Length is: 5cm The width is : 3cm
Example
To access public members of a class, we use the (.)dot operator.
These are members marked with public access modifier.
#include <iostream>
using namespace std; class Phone {
public:
double cost;
int slots;
};
int main() {
Phone Y6;
Phone Y7;
Y6.cost = 100.0;
Y6.slots = 2;
Y7.cost = 200.0;
Y7.slots = 2;
cout << "Cost of Huawei Y6 : " << Y6.cost << endl;
cout << "Cost of Huawei Y7 : " << Y7.cost << endl;
cout << "Number of card slots for Huawei Y6 : " << Y6.slots << endl;
cout << "Number of card slots for Huawei Y7 : " << Y7.slots << endl;
return 0;
}