Data Structures
Lecture 4: Classes in C++
Azhar Maqsood
NUST Institute of Information Technology (NIIT)
Classes in C++
• A class is a means of abstraction in C++
• A class is a specification of a group of objects that
all have the same essential properties
• A variable of class type is known as object and
the operations on that type are called methods
Difference b/w Structures and Classes
A structure is a collection of named fields
whereas a class is a collection of named fields
and methods that apply to objects of that
class type.
Structures do not support data encapsulation
whereas classes do, restricting access to
members of a class to methods of a class itself
FOR EXAMPLE . . .
A class example
class
Student
Properties (data members)
name, graduation year, list of courses,
number of courses
Operations (methods)
List Courses
List Student Information
Add a Course
Set Graduation Date
A C++ definition of a class
class Student
{
private :
char studentName[30];
string courses[6];
int numCourses;
int gradDate;
// declares a class data type
// does not allocate memory
// 4 private data members
public :
// 5 public function members
void AddCourse(string9 CourseName);
void ListCourses(void) const;
void ListInfo(void) const;
void SetGradDate(int year);
Student();
// Constructor Function
};
An object is an instance of a class
For example:
object
studentA
Name: Andrea Student
Graduation Year: 2007
List of Courses: CSCI132, MATH331
Number of courses: 5
Use of C++ data type class
•
Facilitates re-use of C++ code for an ADT.
•
Software that uses the class is called a client.
•
Variables of the class type are called objects or instances
of the class.
•
Client code uses public member functions to handle its
class objects.
•
Private members cannot be directly accessed by client
code. It can only be accessed by member functions.
C++ Data Type class represents
an ADT
• 2 kinds of class members:
data members and function members
• class members are private by default
• data members are generally private
• function members are generally declared public
• private class members can be accessed only by
the class member functions, not by client code.
Implementing Methods
Methods of a class can be implemented
within the declaration of a class or outside it.
Example 1
class Student
{
private :
char studentName[30];
string courses[6];
int numCourses;
int gradDate;
// declares a class data type
// does not allocate memory
// 4 private data members
public :
Student();
// 5 public function members
void AddCourse(string CourseName);
void ListCourses(void);
void ListInfo(void);
void SetGradDate(int year)
{
gradDate = year;
}
};
Example 2
// SPECIFICATION FILE
( student .h )
// Specifies the data and function members.
class Student
{
public:
. . .
private:
. . .
};
// IMPLEMENTATION FILE
( student.cc )
// Implements the Student member functions.
. . .
Implementation file for Student
// IMPLEMENTATION FILE
( student.cc )
// Implements the Student member functions.
#include "student.h"
#include <iostream.h>
// also must appear in client code
. . .
void Student :: SetGradDate (int year )
{
gradDate = year;
}
. . .
Implementing Methods
•
In the heading, the function's name is preceded by the
class name and :: - otherwise C++ won't realize this
is a class’s member function.
Student :: SetGradDate(int year);
{
gradDate = year;
}
•
Within the body of the function, the class’s member
variables and other member functions may all be
accessed.
Client Code Using Student
#include "student.h"
#include <iostream.h>
int main ( void )
{
int newYear;
string newCourse;
Student newStudent;
// includes specification of the class
// create new Student object
cout << "Enter graduation year: ";
cin >> newYear;
newStudent.SetGradDate(newYear);
cout << "Enter course to be added: ";
cin >> newCourse;
newStudent.AddCourse(newCourse);
newStudent.ListInfo();
newStudent.ListCourses();
}
// end of main
// set Graduation date
// Add a course
// List Student information
// List courses
Inheritance
Inheritance: The ability of a class to derive
properties from a previously defined class.
Inheritance is a relationship among classes.
A class can derive the behavior and structure of
another previously defined class.
base class
derived class
The derived class is also called as subclass, child.
The base class is also called as superclass, parent.
Inheritance cont.
In C++, a derived class inherits all the
members of its base class, except the
constructors and destructor.
A derived class has data members and member
functions of the base class in addition to the
members it defines.
A derived class can revise any inherited member
function.
Constructor Arguments
When a constructor does not have to accept
arguments, it is called an object’s default
constructor. Like regular functions,
constructors may accept arguments, have
default arguments, be declared inline, and be
overloaded.
Destructors
A destructor is a member function that is
automatically called when an object is
destroyed.
Destructors have the same name as the class,
preceded by a tilde character (~)
In the same way that a constructor is called then
the object is created, the destructor is
automatically called when the object is destroyed.
In the same way that a constructor sets things up
when an object is created, a destructor performs
shutdown procedures when an object is
destroyed.
Function Overloading
In C++, several functions of the same name
can be defined as long as these function name
different sets of parameters (different types
or different number of parameters).
Object Orientation
In recent years, the object oriented way of
doing things has become increasingly popular.
Object oriented ways of thinking have been
applied in many different areas of computer
science. E.g.
Software system design
Operating systems
Programming languages
O-O design principles
Organise software as a collection of objects
and classes.
Objects provide a set of operations (methodsbehaviour)
Objects have an internal state.
Objects belong to (are instances of) a class.
Example:
class Rectangle
{
private:
float width, length, area;
public:
void setData(float, float);
void calcArea(void);
float getWidth(void);
float getLength(void);
float getArea(void);
};
13.3 Defining Member Functions
Class member functions are defined similarly
to regular functions.
void Rectangle::setData(float w, float l)
{
width = w;
length = l;
}
13.4 Defining an Instance of a Class
Class objects must be defined after the class is
declared.
Defining a class object is called the
instantiation of a class.
Rectangle box;
// box is an instance of Rectangle
Accessing an Object’s Members
box.calcArea();
Pointers to Objects
Rectangle *boxPtr;
boxPtr = &box;
boxPtr->setData(15,12);
© Copyright 2025 Paperzz