



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















































Structures in C Structures in C
Structures in C Structures in C
continue… Structure members or elements Structure name or structure tag
Structures and Classes in Structures and Classes in C++ C++ C++ supports all the features ofC++ supports all the features of structures as defined in C. structures as defined in C. In C++, a structure can have bothIn C++, a structure can have both variables and functions as members. variables and functions as members. It can declare some of its members asIt can declare some of its members as ‘private’. ‘private’. In C++, the structure names are stand-In C++, the structure names are stand- alone and can be used like any other alone and can be used like any other type names. type names. student A; student A; //// C++ declarationC++ declaration
Structures and Classes in Structures and Classes in C++ C++
continue…
Class Declaration Class Declaration
Class Declaration Class Declaration (^) The body of a class isThe body of a class is enclosed within enclosed within braces and terminated braces and terminated by a semicolon. by a semicolon. The class bodyThe class body contains the contains the declaration of declaration of variables and variables and functions. functions. These functions andThese functions and variables collectively variables collectively called class members. called class members. continue… class class_name class class_name { { private : private : variable declarations; variable declarations; function declarations; function declarations; public : public : variable declarations; variable declarations; function declarations; function declarations; }; };
Class Declaration Class Declaration
continue… class class_name class class_name { { private : private : variable declarations; variable declarations; function declarations; function declarations; public : public : variable declarations; variable declarations; function declarations; function declarations; }; };
Class Declaration Class Declaration class class_name class class_name { { private : private : variable declarations; variable declarations; function declarations; function declarations; public : public : variable declarations; variable declarations; function declarations; function declarations; }; }; The variablesThe variables declared inside the declared inside the class are known as class are known as data members data members .. and the functions areand the functions are known as known as membermember functions functions .. Only the memberOnly the member functions can have functions can have access to the private access to the private data members and data members and private functions. private functions. continue…
Class Declaration Class Declaration continue… Data Functions Private area Data Functions Public area No entry to private area X Entry allowed to public area Data hiding in CLASS
Class Example Class Example class item class item { { int int number;number; // variable declaration// variable declaration float float cost;cost; // private by default// private by default public : public : void void getdata( int a, float b);getdata( int a, float b); (^) //// function declarationfunction declaration void void putdata( void );putdata( void ); // using prototype// using prototype }; };
Class Example Class Example
continue… class item class item { { int int number;number; float float cost;cost; public : public : void void getdata(intgetdata(int a, float b); a, float b); void void putdata(void); putdata(void); }; };
Class Example Class Example continue… class item class item { { int int number;number; float float cost;cost; public : public : void void getdata(intgetdata(int a, float b); a, float b); void void putdata(void); putdata(void); }; }; Class : ITEM DATA number cost ……… FUNCTIONS getdata( ) putdata( ) ……… Representation of a class