240 likes | 435 Views
Welcome to Classes and Objects. Prepared By : Nishant Tiwari PGT(CS) JNV, Padmi,Mandla. Classes and Objects. Introduction Classes Data hiding and Encapsulation Functions in a class Using objects Static Class Members. Introduction.
E N D
Welcome to Classes and Objects Prepared By : NishantTiwari PGT(CS) JNV, Padmi,Mandla
Classes and Objects Introduction Classes Data hiding and Encapsulation Functions in a class Using objects Static Class Members
Introduction • The most important features of C++ are the classes and objects. C++ is a bunch of small additions to C, with a few major additions. As the name object-oriented programming suggests this approach deals with objects. Classes are collections of related to a single object type. Classes not include information regarding the real world object but also function to access the data and classes possess the ability to inherit from other classes
Classes • Class represents a group of similar objects • A class is a way to bind the data describing an entity and its associated functions together • A class is user-defined data type used to implement an abstract object giving you the capability to use object oriented programming with c++. • A class includes members • A member can be either data, know as a data member or a function known as a member function
Classes Declaration • Class class_name { Private: data_members member_functions [public: data_members member_functions] [protected: data_members member_functions] }
Classes Declaration • The contents of each instance of class_name : data_members and data_functions • The members are of private, protected or public specifier
Classes Declaration • The declaration of a class involves declaration of its four associated attributes • Data members are the data-type properties that describe the characteristics of a class. There may be zero or more data members of any type in a class • Member functions are set of operations that may be applied to object of that class. There may be zero or more member functions of a class. They are referred to as the class interface • Program Access Levels that control access to members from within the program. These access levels are private, protected or public .Depending upon the access level of a class member, access to it allowed or denied • Class Tag name that serves as a type specifier for the class using which objects of this class type can be created.
Classes Declaration • The class specification takes place in two parts Class definition: which describe the component members (data member and functions members) of the class Class method: which describe how certain class member function are implemented.
The class methods definition • Outside the class definition • Inside the class definition
Outside the class definition Class salesman { Int salesman_no: Int product_no; Public: Void readdata( ); Void displaydata( ); }; Void salesman::readdata( ) { Cout<<“enter salesman_no”; Cin>>salesman_no; } Void salesman::displaydata( ) { Cout<<“the salesman_no”<<salesman_no; }
Outside the class definition • A member function definition outside the class definition is much same as that of function definitions . The only difference is that the name of the function is the full name of the function. the full name (also called qualified-name) of the function is written as: • Class-name::function-name
Outside the class definition • Class-name:indicate that the function specified by function_name is a member of the class specified by class-name. • The symbol :: called the scope resolution operator specifies that the scope of the function is restricted to the class class-name.
Outside the class definition • The general form of a member function definition outside the class definition is Return-type class-name :: function_name (parameter list) { Function body }
Membership label • The membership label (classname ::) will resolve their identity and scope Void student : : add ( )
inside the class definition A function define inside a class is automatically inline function. Class student { Int a; Public: Void readdata () { Cout<<“enter the value of a”; Cin>>a; }
Referencing class members • A class specification does not define any objects of its type rather it just define the properties of a class example • Student s1,s2,s3;
Referencing class members • Class abc { Int x,y // private by default Public: Int z; Int add (int a,int b) { Int c=a+b; Return c; } }; Abc a1,a2;
Referencing class members • The private data of a class can be accessed only through the member function of that class • The public data can be accessed by the non-member function through the objects of the class
Referencing class members • The general format for calling a member function is Object-name.function-name(actual-arugment); A1.abc(2,3); A2.abc(4,6);
Referencing class members • To access the public data member,the format used is: a1.z or a2.z But if we give A1.x or a2.y An error since x and y are private data member of class abc
Array with in class • An array in a class can be private or public data member of the class • A class can have an array as its member variable • If an array happen to be private data member of the class then only the member functions of the class can access it. • If an array is a public data member of the class it can be accessed directly using object of this class type.
Array with in class Class exarray { Int arr[10]; //private data member Public: Int largest(void); Int sum(void); }; The data member of class exarray accessed only through the member function largest() and sum(); not directly by objects.
Question classes and objects • Q1 what is the significance of scope resolution operator (::) ? • Q2 what is difference between private and public member of a class ? • Q3 Define abstract data type and encapsulation ? • Q4 Declare a class employee having following members: Data member: employee number Employee name Employee address Employee department Member function To read data member To display