Classes and objects
Download
1 / 25

Chapter - 4 - PowerPoint PPT Presentation


  • 75 Views
  • Uploaded on

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 : <variable declaration>;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Chapter - 4' - cole-ware


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

Classes and Objects

Chapter - 4

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


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

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


The class definition
The Class Definition

class <class_name>

{ private : <variable declaration>;

<function declaration>;

protected : <variable declaration>;

<function declaration>;

public : <variable declaration>;

<function declaration>;

};

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Class method definition
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; }

    };

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Referencing class members
Referencing Class Members

  • The members of class are referenced using object of the class.

  • Eg: for above class XYZ if ob1 is the object then to access class members

    ob1.enter( );

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Arrays within the class
Arrays within the class

  • class Exarray

    { intarr[10];

    public :

    int largest( void);

    int sum(void);

    };

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Scope of class and its members
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

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


The scope rules and classes
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 class

    • its object can not be declared anywhere in program.

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Global vs local object
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.

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Global vs local class
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

:

}

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Types of class functions
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 { intrno;

    public:

    Student( ) //Manager Function

    { rno=0; }

    void getrno( ) //Accessor Function

    { return rno; }

    void enter( ) //Mutator Function

    { cin>>rno; }

    };

  • MutatorFunction:a member function that allows to change the data member of an object.

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

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


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

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Inline function
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);

    :

    }

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Constant member function
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;

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Nesting of member functions
Nesting of Member Functions

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

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Memory allocation of objects
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.

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Static class members
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.

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Example
Example:

class X

{ static int count;

:

static void show( void )

{ cout<<count; }

:

};

int X :: count;

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Board 2011
BOARD - 2011

Define a class Applicant in C++ with following description:

  • A data member Ano (Admission no) of type long

  • A data member Name of type String

  • A data member Agg (Aggregate Marks) of type float

  • A data member Grade of type char

  • A 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.

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


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

};

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Board 2010
BOARD - 2010

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

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


class STOCK

{ private : intIcode;

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

};

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Board 2009
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( )

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


class HOTEL

{ intRno;

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( );

}

};

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


Board 2008
BOARD - 2008

Define 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 foGprice 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.

Made by : Mayuri Patel,PGT(CS),KV EME BARODA


ad