1 / 25

Chapter - 4

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

rose-horn
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes and Objects Chapter - 4

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

  3. The Class Definition class <class_name> { private : <variable declaration>; <function declaration>; protected : <variable declaration>; <function declaration>; public : <variable declaration>; <function declaration>; };

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

  5. Referencing Class Members • The members of class are referenced using object of the class. • Eg: for above class XYZ ob1.enter( );

  6. Arrays within the class • class Exarray { intarr[10]; public : int largest( void); int sum(void); };

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

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

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

  10. 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 : }

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

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

  13. 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); : }

  14. 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;

  15. Nesting of Member Functions • When a member function is called by another member function, it is called nesting of member functions.

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

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

  18. Example: class X { static int count; : static void show( void ) { cout<<count; } : }; int X :: count;

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

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

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

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

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

  24. 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( ); } };

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

More Related