Classes
Classes
1
– Primitive data types (int, float, char, ..) for storing any type of data.
2
Process of defining ADTs
Built-in data types
Float, int, char, double etc.
Constructs to define new ones:
User defined data types
Student, complex num etc using struct
Operations:
Defining Processes
Functions for Operations – member functions
Put things together:
Encapsulation
Bundling things together
Define ADTs:
Abstract Data Types
Class and access specifiers
3
Complex numbers
• How to represent a complex number?
• As two independent numbers – real, imag.
• A structure with two numbers as members
– struct complex { float real; float imag;} a, b;
– a.real = 4.5; a.imag = 2.0;
• In both the cases how to add, subtract or multiply two
complex numbers.
• Write functions for each of these operations.
• There is no direct relationship between the definition of
structure complex and the functions performing the
operations.
NITW – PSCP 31 4
Class
• Class is a user defined data type, which holds its own data
members and member functions, which can be accessed and used
by creating instance of that class.
• The variables inside class definition are called as data members
and the functions are called member functions.
• Class in C++ programming language is a mechanism to define a
user defined data type achieving:
✓ Definition of data items – members
✓ Definition of associated operations as functions – member
functions
✓ Putting the data definition and member functions together –
encapsulation
✓ Possible to hide data definitions and / or implementation details of
member functions – information hiding.
• A user defined data type using class can be termed as Abstract Data
Type (ADT).
CLASS
• A data abstraction is a simplified view of a real world object that
– includes only features one is interested in (the operations of data object).
– while hides away the unnecessary details (hides how these operations are
implemented).
7
Access Control in C++
• Access modifiers in C++ class defines the access control rules.
• C++ has 3 new keywords introduced, namely,
– public
– private
– protected
class Circle
{
private:
float radius; data member (accessible
public:
through class methods)
void setRadius(float r);
float getDiameter();
float getArea();
float getCircumference(); They are accessible from
}; outside the class, and they can
access the member (radius)
This class example shows how we can encapsulate (gather) a circle information into
one package (class)
10
Example
Example – Consider the real world entity Person.
Name of the class
class Person
{
private:
char name[15]; data members (accessible
Date DOB; through class methods)
public:
void setDOB(int mm,
int dd,
int yy); They are accessible from
int getAge(); outside the class, and they can
}; access the members (name,
DOB)
NITW – PSCP 31 11
Creating 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,
we need to create objects.
Syntax to Define Object in C++
className objectVariableName;
Example:
// Create a class
class Student
{
public:
int rollno;
string name;
};
Object to a class can be created in 2 ways:
1. // Here A and B are objects created after declaration of class
class Student
{
public:
int rollno;
string name;
}A,B;
2. // Here A and B are objects declared in main()
int main()
{
Student A;
Student B;
}
Accessing Data Members of Class
Note:
1. By default, all members of a class are private
if you don't specify an access specifier.
2. Once class is created any number of objects
can be created for the class
Class Methods
• Methods are functions that belongs to the class.
• There are two ways to define functions that
belongs to a class:
– Inside class definition
– Outside class definition
• Inside class definition
Member functions are the functions, which have
their declaration inside the class definition and
works on the data members of the class. The
definition of member functions can be inside or
outside the definition of class.
Example
#include<iostream>
using namespace std;
class student
{
public:
int x,y;
public:
void display()
{
cout<<"welcome to NITW"<<endl;
}
};
int main()
{
student s1;
s1.display();
return 0;
}
• Outside class Definition
• To define a function outside the class
definition, you have to declare it inside the
class and then define it outside of the class.
This is done by specifying the name of the
class, followed the scope resolution ::
operator, followed by the name of the
function
• If we plan to define the member function
outside the class definition then we must
declare the function inside class definition and
then define it outside.
Example :
#include<iostream>
using namespace std;
class student
{
public:
int x,y;
public:
void display();
};
void student::display()
{
cout<<"welcome to NITW"<<endl;
}
int main()
{
student s1;
s1.display();
return 0;
}