Top Banner
CHAPTER - 4 Classes and Objects
25

Chapter - 4

Jan 02, 2016

Download

Documents

Rose Horn

Classes and Objects. Chapter - 4. Class. A class is a way to bind the data describing an entity and its associated functions together. In C++ class makes a data type that is used to create objects of this type. The Class Definition. class < class_name > {private :; - PowerPoint PPT Presentation Copyright Complaint Adult Content Flag as Inappropriate Report This Download Presentation Chapter - 4
An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - Presentation Transcript Classes and Objects Chapter - 4 Class
  • A class is a way to bind the data describing an entity and its associated functions together.
  • In C++ class makes a data type that is used to create objects of this type.
The Class Definition class {private :; ; protected : ; ; public : ; ; };
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter - 4

CHAPTER - 4Classes and Objects

Page 2: Chapter - 4

Class

A class is a way to bind the data describing an entity and its associated functions together.

In C++ class makes a data type that is used to create objects of this type.

Page 3: Chapter - 4

The Class Definition

class <class_name>

{ private : <variable declaration>;

<function declaration>;

protected : <variable declaration>;

<function declaration>;

public : <variable declaration>;

<function declaration>;

};

Page 4: Chapter - 4

Class Method Definition Outside the Class Definition

return_type <class_name> :: <func_name> (parameter list)

{ : }

Eg:

class XYZ

{ private : int a;

public : void enter( );

};

void XYZ ::enter ( )

{ cin>>a; }

Inside the Class Definition

class XYZ

{ private : int a;

public : void enter( )

{ cin>>a; }

};

Page 5: Chapter - 4

Referencing Class Members The members of class are referenced

using object of the class.

Eg: for above class XYZ

ob1.enter( );

Page 6: Chapter - 4

Arrays within the class

class Exarray

{ int arr[10];

public :

int largest( void);

int sum(void);

};

Page 7: Chapter - 4

Scope of Class and Its members

Public Members : the data members and member functions are directly accessed by any function.

Private Members : the members are not accessible outside the class but accessible only by the member functions, and not inherited

Protected Members : the members are not accessible outside the class as Private members but are inherited to derived classes

Page 8: Chapter - 4

The scope rules and classes Global Class

if a class definition occurs outside any function is called global class.

its object can be declared anywhere within the program.

Local Class if a class definition occurs inside any

other function is called local classits object can not be declared anywhere in

program.

Page 9: Chapter - 4

Global Vs. Local Object If an object is declared outside any function then

its called global object

While an object declared within another function then its called local class.

A global object can be declared only from global class

While a local object can be declared from a local class.

Page 10: Chapter - 4

Global Vs. Local Class

class XYZ // global class

{ int a;

public :

void enter( );

}ob1; //global object

void main ( )

{ XYZ ob2; //local object

:

}

void main ( )

{

class XYZ //local class

{ int a;

public :

void enter( );

}ob1; //legal local object

}

void Func( )

{

XYZ ob2; // illegal

:

}

Page 11: Chapter - 4

Types of Class Functions

Accessor Function: the function that allow user to access the data member of object but cannot change the value of data member.

Eg: class Student { int rno;

public:

Student( ) //Manager Function

{ rno=0; }

void getrno( ) //Accessor Function

{ return rno; }

void enter( ) //Mutator Function

{ cin>>rno; }

}; Mutator Function:a member function that allows to change the

data member of an object.

Manager Function : member function which initializes and destroying class objects.

Page 12: Chapter - 4

Nested Classes A class declared within another class is called nested class

or an object of one class declared inside another class is called Nested Class.

Eg:

class X

{ :

class Y

{ : };

};

Eg:

Class X { ………. };

class Y { X ob1; };

Page 13: Chapter - 4

Inline Function A function definition occurs inside the class definition An inline function definition should be placed above all the functions that call

it. Are best for small functions which are called often. Eg:

class X

{ int a;

public :

inline void square( int I )

{ cout<<I * I; }

};

void main( )

{ X ob1;

ob1.square(5);

:

}

Page 14: Chapter - 4

Constant Member Function If a member function of a class doesnot

alter any data in the class then this member function may be declared as a constant member function using the keyword const.

Eg: int Maxi (int, int) const;

Page 15: Chapter - 4

Nesting of Member Functions

When a member function is called by another member function, it is called nesting of member functions.

Page 16: Chapter - 4

Memory allocation of objects Member functions are created and placed in the

memory space only once when the class is defined.

The memory space is allocated for objects’ data members only when the objects are declared.

No separate space is allocated for member functions when the objects are created.

Separate memory space is allocated to objects at the time of their declaration for their data members.

Page 17: Chapter - 4

Static Class Members

Static Data Member : its like a global variable which is globally available for all the objects of that class type.

A static data member must be defined outside the class definition.

Difference between Static & General Data member:There is only one copy of this data member maintained

for the entire class.It is visible only within the class.

Static Member function: a member function that accesses only the static members of a class may be declared as static. Put static keyword before the function declaration in the class definition.

Page 18: Chapter - 4

Example:

class X

{ static int count;

:

static void show( void )

{ cout<<count; }

:

};

int X :: count;

Page 19: Chapter - 4

BOARD - 2011Define a class Applicant in C++ with following description:

A data member Ano (Admission no) of type longA data member Name of type StringA data member Agg (Aggregate Marks) of type floatA data member Grade of type charA member function GradeMe( ) to find the Grade as per the Aggregate

Marks obtained by a student. Equivalent Aggregate marks range and the respective Grades are shown as follows

Aggregate marks Grade

>= 80 A

Less than 80 and >=65 B

Less than 65 and >= 50 C

Less than 50 and >=33 D

Less than 33 E

Public members:A function Enter( ) to allow user to enter values for Ano, Name, Agg & call

function GradeMe( )to find the Grade.A function Result( ) to allow user to view the content of all the data

members.

Page 20: Chapter - 4

class Applicant

{ long Ano;

char Name[20 ];

float Agg;

char Grade;

void GradeMe( )

{ if(Agg>=80) Grade = ‘A’;

elseif( Agg>=65 && Agg<80 ) Grade = ‘B’;

else if (Agg>=50 && Agg < 65) Grade = ‘C’;

else if (Agg >=33 & Agg <50 ) Grade = ‘D’;

else Grade = ‘E’;

}

Public : void Enter( )

{ cout<<“Enter Ano;”; cin>>Ano;

cout<<“Enter Name”; gets(Name);

cout<<“Enter Aggregate marks”; cin>>Agg;

GradeMe( );

}

void Show( )

{ cout<<Ano <<Name << Agg <<Grade; }

};

Page 21: Chapter - 4

BOARD - 2010Define a class STOCK in C++ with the following description:

Private members : Icode of type integer (Item Code) Item of type string (Item Name)Price of type float (Price of each item)Qty of type integer (Quantity in stock)Discount of type float (Discount % on the item)A member function FindDisc( ) to calculate discount as per the

following rule:

if Qty < = 50 Discount is 0

if 50 < Qty <=100 Discount is 5

if Qty>100 Discount is 10

Public Members:A function Buy( ) to allow user to enter values for ICode, Item, Price,

Qty and call function FindDisc( ) to calculate the Discount.A function ShowAll( ) to allow user to view the content of all the data

members.

Page 22: Chapter - 4

class STOCK

{ private : int Icode;

char Item[20];

flaot Price;

int Qty;

float Discount;

void FindDisc( )

{ if(Qty <=50) Discount = 0;

else if(Qty >50 && Qty<=100)

Discount = 5;

else Discount = 10;

}

public : void Buy( )

{ cout<<“Enter Icode”; cin>>Icode;

cout<<“Enter Item’; gets(Item);

cout<<“Enter Price”; cin>>Price;

cout<<“Enter Qty”; cin>>Qty;

FindDisc( );

}

void ShowAll( )

{ cout<<ICode << Item <<Price << Qty << Discount; }

};

Page 23: Chapter - 4

BOARD - 2009

Define a class HOTEL in C++ with the following description:

Private Members:

Rno //Room No

Name //Customer name

Tariff //stores per day charges

NOD //Number of days of stay

Calc( ) // a function to calculate and return amount as NOD*Tariff and if NOD * Tariff is more than 10000

then as 1.05*NOD*Tariff

Public members:

checkin( ) // a function to enter the Rno, Name, Tariff and NOD

checkout( ) // a function to display Rno, Name, Tariff, NOD and Amount by calling function CALC( )

Page 24: Chapter - 4

class HOTEL

{ int Rno;

char Name[20];

float Tariff;

int NOD;

float CALC( )

{ if(NOD * Tariff > 10000 )

return 1.05*NOD*Tariff;

else

return NOD*Tariff;

}

public :

void checkin( )

{ cin>>Rno; gets(Name); cin>>Tariff; cin>>NOD; }

void checkout( )

{cout <<Rno<<Name<<Tariff<<NOD;

cout<<“Amount = “<<CALC( );

}

};

Page 25: Chapter - 4

BOARD - 2008Define a class Clothing in C++ with the following description

Private members :Code of type string

Type of type String

Size of type integer

Material of type String

Price of type float

A function Calc_Price( ) which calculates and assigns the value fo Gprice as follows:

For the value of Material as “COTTON”

Type Price (Rs)

TROUSER 1500

SHIRT 1200

For material other than “COTTON” the above mentioned price gets reduced by 25%

Public members :

A constructor to assign initial values of Code, Type, and Material with the word “NOT ASSIGNED” and size and price with 0

A function ENTER( ) to input the values of the data members Code, Type, Size and Material and invoke the Calc_Price( ) function.

A function Show( ) which displays the content of all the data members for a clothing.