Cpp Class 2

Download as
 PPT
Presentation Description 

No description available

Views: 206
Like it  ( Likes) Dislike it  ( Dislikes)
Added: December 24, 2008 This Presentation is Public 
Presentation Category : Education All Rights Reserved
Presentation Statistics
Views on authorSTREAM: 205 | Views from Embeds: 1
Others - 1 views
Presentation Transcript

CHAPTER - II :CHAPTER - II


CLASSES & OBJECTS :CLASSES & OBJECTS Classes in C++ define types of data structures and the functions that operate on those data structures. Instances of the class are called objects. While the variables and functions in the definition of the class are called members. Classes are fundamental components of a C++ program. Like structs, introduced earlier, they group related information together. They are essentially new user-defined data types, but they can also contain member functions that operate on their data members.


Slide 3:#include class person { char name[30]; int age; public: void getdata(void); void display(void); }; void person :: getdata(void) { cout>name; cout>age; } void person :: display(void) { cout<<“\nName:”<

A Simple Class :A Simple Class A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.


Syntax for a class :Syntax for a class Classes are generally declared using the keyword class, with the following format: class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names; Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.


A Simple Class :A Simple Class #ifndef _IMAGE_H_ #define _IMAGE_H_ #include #include "vectors.h“ class Image { public: ... private: ... }; #endif Prevents multiple references Include a library file Include a local file Variables and functions accessible from anywhere Variables and functions accessible only from within this class’s functions


Access Specifiers :Access Specifiers An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire: private members of a class are accessible only from within other members of the same class or from their friends. protected members are accessible from members of their same class and from their friends, but also from members of their derived classes. Finally, public members are accessible from anywhere where the object is visible.


Slide 8:Data Members Member Functions Data Members Member Functions Private Area Public Area CLASS Entry is allowed to public area Entry is not allowed to private area


Slide 9:Creating Objects class item { int number; //variable declaration float cost; //private by default Public: int z; void getdata(int a,float b); //functions declaration void putdata(void); //using prototype }; item x; // memory for x is created item x,y,z;


Slide 10:Accessing class members Object_name.function_name (actual arguments); x.getdata(100,75.5); //legal x.putdata(); //legal x.Number = 100; // illegal; y.z = 250; //legal


Slide 11:class CRectangle { int x, y; public: void set_values (int,int); int area (void); } rect; Class Private data members Public member functions Object name By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access. For example:


Slide 12:rect.set_values (3,4); myarea = rect.area(); We can refer within the body of the program to any of the public members of the object rect as if they were normal functions or normal variables, just by putting the object's name followed by a dot (.) and then the name of the member. The only members of rect that we cannot access from the body of our program outside the class are x and y, since they have private access and they can only be referred from within other members of that same class.


Slide 13:#include using namespace std; class CRectangle { int x, y; public: void set_values (int,int); int area () { return (x*y); } }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); return 0; }


Slide 14:What is a scope resolution operator? int count = 0; int main(void) { int count = 0; ::count = 1; // set global count to 1 count = 2; // set local count to 2 return 0; } The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope. A scope resolution operator (::), can be used to define the member functions of a class outside the class. The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example:


Slide 15:You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator. In the following example, the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator. #include using namespace std; class X { public: static int count; }; int X::count = 10; // define static data member int main () { int X = 0; // hides class type X cout << X::count << endl; // use static member of class X }


Slide 16:// example: one class, two objects #include using namespace std; class CRectangle { int x, y; public: void set_values (int,int); int area () { return (x*y); } }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect, rectb; rect.set_values (3,4); rectb.set_values (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }


Slide 17:In this concrete case, the class (type of the objects) to which we are talking about is CRectangle, of which there are two instances or objects: rect and rectb. Each one of them has its own member variables and member functions. Notice that the call to rect.area() does not give the same result as the call to rectb.area(). This is because each object of class CRectangle has its own variables x and y, as they, in some way, have also their own function members set_value() and area() that each uses its object's own variables to operate.


Defining Member Functions :Defining Member Functions Outside class definition Inside class definition


Slide 19:Outside the Class Definition Return_type class_name :: function_name(argument declaration) { Function body } Return_type function_name(argument declaration) { Function body } Member functions that are declared inside a class have to be defined separately outside the class. Their defn are very much like normal functions. They should have a function header and function body. The diff b/w the member function and a normal function is that a member function incorporates a membership ‘identity label’ in the header.


Slide 20:The member functions have some special characteristics that are often used in the program development. Several different classes can use the same function name. The membership label will resolve their scope. Member functions can access the private data of the class. The non member function cannot do so. A member function can call another member function directly without using the dot operator


Slide 21:Inside a class definition Class item { int number; float cost; Public: void getdata(int a,float b); //declaration //inline function void putdata(void); //definition { Cout<