1 / 17

Class and Objects

Class and Objects. Topics to be Discussed…. Class Classes in C++ Class Example Object of a Class Accessing Class Members Defining Member Functions Memory Allocation for Objects. Class. A Class is a user defined datatype which holds both data members and member functions.

hope
Télécharger la présentation

Class and Objects

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

  2. Topics to be Discussed… • Class • Classes in C++ • Class Example • Object of a Class • Accessing Class Members • Defining Member Functions • Memory Allocation for Objects

  3. Class • A Class is a user defined datatype which holds both data members and member functions. • Data members are data items related to Class and Member Functions are functions related to class which indicate different operations that can be performed on data members. BACK

  4. Classes in C++ • A class definition begins with the keyword class. • The body of the class is contained within a set of braces, { } ; class class_name { …. …. …. }; Any valid identifier Class body (data member + methods)

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

  6. Classes in C++ class class_name { private: … … … public: … … … }; private members or methods Public members or methods

  7. Classes in C++ • Member access specifiers • public: • can be accessed inside as well as outside the class. • private: • Accessible only to member functions of class • Private members and methods are for internaluse only. BACK

  8. Class Example • This class example shows how we can encapsulate (gather) a student information into one package (unit or class) • Class student { private: int roll; char name[25]; public: void getdata(); void display(); }; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the Data members BACK

  9. Object of a Class • Declaring a variable of a class type creates an object. You can have many variables of the same type (class). • Instantiation • Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code • Syntax: classname listof objects; student x,y;

  10. Objects can also be created when a class is defined by placing their names immediately after the closing brace. Class student { private: int roll; char name[25]; public: void getdata(); void display(); }x,y; BACK

  11. Accessing Class Members • Any Public Member of the class can be accessed with the help of following syntax: a) object name.membername; b) objectname.functionname(actual arguments); Class Abc { private: int a; int b; public: int c; void sum(); }; Void main() { Abc x; x.c=101; // accessing the data member x.a=101; // error,access denied because a is a private member x.sum(); // accessing the member function } BACK

  12. Defining Member Functions • Member functions can be defined in two ways: • Member functions defined outside class • Using scope resolution operator (::) • “Ties” member name to class name • Uniquely identify functions of particular class • Different classes can have member functions with same name • Format for defining member functions ReturnType ClassName::MemberFunctionName( ) { … }

  13. Member function define outside the class definition Class Abc { private: int a; int b; public: int c; void sum(); }; Void Abc :: sum() { Cout<<“sum is =“<<a+b; }

  14. Member function define inside the class definition • Member functions defined inside the class • Can be defined within the class at the time of declaration • Without Using scope resolution operator (::) Class Abc { private: int a; int b; public: int c; void sum() { Cout<<“sum is =“<<a+b; } };

  15. #include<iostream.h> #include<conio.h> Class abc { int a,b,c; Public: Void getdata(int,int); Void putdata(); Void sum(); }; Void abc::getdata(int I,int j) { a=i; b=j; } Void abc::putdata() { Cout<<“sum is =“<<c; } Void abc::sum() { C=a+b; }}; Void main() { Abc x; x.getdata(1,2); x.sum(); x.putdata(); } BACK

  16. Memory Allocation for Objects • Memory is allocated to each of the object for data members.data members of each object are stored seperately but member functions are stored only once. • Space is allocated to member functions when class is declared. • Space is allocated for data members when objects are created. BACK

  17. THANKS

More Related