170 likes | 390 Views
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.
E N D
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. • 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
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)
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.
Classes in C++ class class_name { private: … … … public: … … … }; private members or methods Public members or methods
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
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
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;
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
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
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( ) { … }
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; }
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; } };
#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
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