Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 62

EC6132D Data Structures and Algorithms

Structures and Classes


Course Faculty: Dr Ameer P M
ECE Department
ameer@nitc.ac.in
Mob: 9447536264
ECE Block I – Room No: 401
DATA STRUCTURES

• We have already learned how groups of same data types can


be used in C++.

• But we also need sets


of different elements with different data types.
DATA STRUCTURES
• A data structure is a group of data elements grouped together under
one name.
• These data elements, known as
members, can have different types and different lengths.
• The syntax is

struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
THE FIRST THING WE HAVE TO KNOW
IS THAT…….
• a data structure creates a new type:

• Once a data structure is declared, a


new type with the identifier specified as structure_name is
created and can be used in the rest of the program as if it was
any other type like
int a;
char b;
WHAT IS DONE HERE ?
DECLARING OBJECTS
OPERATING WITH MEMBERS
‘STRUCTURES’ HAVE DATA

• Structures are used to represent a record, suppose you want to


keep track of your books in a library.
• You might want to track the following attributes about each
book
• Title
• Author
• Subject
• Book ID
AN EXAMPLE STRUCTURE
TO ACCESS

• book.title
• book.author
• …………..
• …..
NOTATIONS FOR ACCESS- THESE
ARE EQUIVALENT
• a->b
• (*a).b

• a.b is only used if b is a member of the structure .So for a.b, a


will always be an actual object (or a reference to an object) of
a class.
• a->b is essentially a shorthand notation for (*a).b, ie, if a is a
pointer to an object, then a->b is accessing the property b of
the object that a points to.
CASE 1 CASE 2
• Case 1
• std.name
• std.roll

• Case2
• ptr ->name
• ptr->roll
THE -> IS CALLED THE ARROW
OPERATOR
• It is formed by using the minus sign followed by a greater
than sign.
• To access members of a structure, use the dot operator.
• To access members of a structure through a pointer, use
the arrow operator.
CLASS

• A class is an expanded concept of a data structure: instead of


holding only data, it can hold both data and functions.
• So, Classes are user-defined (programmer-defined) types.
• Data (data members)
• Functions (member functions or methods)
• In other words, they are
structures + functions
CLASSES IN C++

• A class definition begins with the keyword class.


• The body of the class is contained within a set of braces,
{ } ; (notice the semi-colon).

class class_name
{
….
Any valid
….
identifier
….
};
Class body (data member +
methods)
Figure 10-2 Class access specifiers
OBJECT

• An object is an instance of a class.

• In terms of variables, a class would be the type, and an object


would be the variable.
CLASSES IN C++

• Within the body, the keywords private: and public: specify the
access level of the members of the class.
• the default is private.

• Usually, the data members of a class are declared in the


private: section of the class and the member functions are in
public: section.
CLASSES IN C++

class class_name
{ private members or methods
private:



public: Public members or methods



};
• Any member that is declared before one other class specifier
automatically has private access.
CLASS METHODS DEFINITION

• Providing code for the member functions


I) outside the class definition
ii) Inside the class definition

In both cases ,the code is the same


INLINE FUNCTIONS ???

• Putting the code inside is called ‘inlining’


• Functions defined inside the class specifications are
automatically ‘inline’
FUNCTIONS DEFINED
OUTSIDE THE CLASS DEFINITION
return type class name:: function name (parameter list)

:: is the scope resolution operator


EXAMPLE
OUTPUT

• area:12
POINTS

• rect is an object of type CRectangle just like saying int x

• CRectangle is the class name (i.e., the type), whereas rect is


an object of type CRectangle
What do these functions Do?
• rect.set_values (3,4); • Gives initial values for x
• myarea = rect.area(); and y

• Calculates the area


using the values of x
and y
• After the previous declarations of CRectangle and rect, we can
refer within the body of the program to any of the public
members of the object rect as if they were normal
functions or normal variables, just by putting the object's
name followed by a dot (.) and then the name of the member.
• All very similar to what we did with plain data
structures before.
• we cannot access x and y using any other function

• they have private access and they can only be referred from
within other members of that same class.
CLASSES IN C++

• Member access specifiers


• public:
• can be accessed outside the class directly.
• private:
• Accessible only to member functions of class
• Private members and methods are for internal use only.

• Protected …………?
WHY SUCH ACCESS SPECIFIERS ???

• For easy debugging..

• Any other reason????????????????


• class Box
• {
• double width;
• public:
• double length;
• void setWidth( double wid );
• double getWidth( void );
• };
CREATE A CLASS NAMED BANK

• With data members


account no , …public member
these are private data
deposit,withdrawal
interest rate
balance
And methods (functions)
getvalues
calc_amount
USING PUBLIC DATA
USING PRIVATE DATA
OUTPUT
ACCESSING MEMBERS

class Abc
{
int x,y ;
public:
int z;
int add (inta ,int b)
{
int c=a+b;
return c ;
}
int sub (int a,int b)
{ int c =a-b;
return c;
};
Abc O1,O2;
PUBLIC AND PRIVATE MEMBERS

• The private data ----thru member functions only


• Public data ----thru non-member functions also
TO CALL A MEMBER FUNCTION…

object name.function name {actual arguments)


• Examples
O1.add(2,3) //O1 and O2 are objects of the class Abc
O2.add(4,5) //
TO ACCESS THE PUBLIC DATA MEMBER

object name.public data member


Example
O1.z or O2.z
But
O1.x and O2.y gives error
???
STATEMENTS -VALID OR NOT??

• O1.sub(3,7) …..
• O2.add (6,8) ………
• sub (16,8) …………
• O1.z=6 …………….
• O2.x =9 ……………..
• O1.y =7 ………….
• O2.z =5 ……………..
CLASS EXAMPLE

• This class example shows how we can encapsulate (gather) a


circle information into one package (unit or class)

No need for others classes to access and


class Circle retrieve its value directly. The
class methods are responsible for
{
that only.
private:
double radius;
public:
void setRadius(double r); They are accessible from outside
double getDiameter(); the class, and they can access the
double getArea(); member (radius)
double getCircumference();
};
# include <iostream>
using namespace std;
class sample
{
int a;
int b;
public:
void setvalue () { a=2.5;b=4.6;}
friend float mean (sample s);
};

float mean (sample s)


{
return float (s.a+s.b)/2.0;
}
MAIN FUNCTION

int main()
{
sample K:
K.setvalue();
cout<<“mean value= “ << mean(K) <<endl;
return 0;
}
• Write example cases of two classes using a friend. The
function can be a member function for one class and a friend
for the other class.
FUNDAMENTALS OF OOP

•  Class
•  Object
•  Encapsulation
•  Abstraction
•  Inheritance
•  Polymorphism
• 
REASONS FOR OOP

1. Simplify programming
2. Interfaces
• Information hiding:
• Implementation details hidden within classes themselves
3. Software reuse
• Class objects included as members of other classes

You might also like