0% found this document useful (0 votes)
11 views

Classes

Uploaded by

depanimokshith
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)
11 views

Classes

Uploaded by

depanimokshith
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/ 24

Lecture 05:

User defined data types - class


Dr. Manjubala Bisi
Assistant Professor,
Department of CSE,
NIT Warangal.

1
– Primitive data types (int, float, char, ..) for storing any type of data.

– Arrays for storing collection of homogeneous data items under a


common identifier.

– Structures for storing the collection of heterogeneous data items


under a common identifier.

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).

• Goals of new data type


– Combining Attributes and Behaviors (Encapsulation).
– Hiding unnecessary details (Information hiding).

• Encapsulation - It is a mechanism that associates the code (Behavior) and


the data (Attribute) it manipulates into a single unit to keeps them safe from
external interference and misuse (C++ provides new data type class to achieve this).

• Information Hiding - Data hiding means to secure data or sometimes


specific behaviors from direct access (C++ provides Access Specifies for this purpose).
6
CLASS
• Abstract Data Type (ADT) is the key to Object-Oriented programming.
An ADT is a set of data together with the operations on the data .

• A class is often used to describe an ADT in C++. A class is also called


as User-Defined Data Type (structures + functions).

class class_name Any valid identifier


{
….
…. • Class body
• (data members + methods )
…. • (access specifiers)
};

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

public Access Modifier


• The public keyword is used to create public members (data and functions).
• The public members are accessible from any part of the program.

private Access Modifier


• The private keyword is used to create private members(data and functions).
• The private members can only be accessed from within the class.
• However, friend classes and friend functions can access private members.

protected Access Modifier


• protected keyword is used in inheritance concept.
• The protected members can be accessed within the class and from the
derived class.
Example
Example – Consider the real world entity circle.
Name of the class

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

• If it’s public, then the data member can be


easily accessed using the direct member
access (.) operator with the object of that
class.
• If the data member is defined as private or
protected, then we cannot access the data
variables directly. Then we will have to create
special public member functions to access,
use or initialize the private and protected data
members
Example 1:
// creating objects at the time of class declaration
#include<iostream>
#include<string.h>
using namespace std;
class hello
{
public:
int rollno;
string name;
}p1,p2;
int main()
{
cout<<"Enter student 1 details"<<endl;
cout<<"Enter rollno"<<endl;
cin>>p1.rollno;
cout<<"Enter name"<<endl;
cin>>p1.name;
cout<<"Enter student 2 details"<<endl;
cout<<"Enter rollno"<<endl;
cin>>p2.rollno;
cout<<"Enter name"<<endl;
cin>>p2.name;
cout<<"Student 1 details"<<endl;
cout<<"Roll no="<<p1.rollno<<endl;
cout<<"Name="<<p1.name<<endl;
cout<<"Student 2 details"<<endl;
cout<<"Roll no="<<p2.rollno<<endl;
cout<<"Name="<<p2.name<<endl;
return 0;
}
Example 2: // creating objects in main()
#include<iostream>
#include<string.h>
using namespace std;
class Hello
{
public:
int rollno;
string name;
};
int main()
{
Hello p1,p2;
cout<<"Enter student 1 details"<<endl;
cout<<"Enter rollno"<<endl;
cin>>p1.rollno;
cout<<"Enter name"<<endl;
cin>>p1.name;
cout<<"Enter student 2 details"<<endl;
cout<<"Enter rollno"<<endl;
cin>>p2.rollno;
cout<<"Enter name"<<endl;
cin>>p2.name;
cout<<"Student 1 details"<<endl;
cout<<"Roll no="<<p1.rollno<<endl;
cout<<"Name="<<p1.name<<endl;
cout<<"Student 2 details"<<endl;
cout<<"Roll no="<<p2.rollno<<endl;
cout<<"Name="<<p2.name<<endl;
return 0;
}
The differences between public and private members:
#include<iostream>
using namespace std;
class MyClass
{
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};
int main()
{
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
If you try to access a private member, an error
occurs:
error: y is private

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;
}

You might also like