properties

12
properties properties Methods of a class can manipulate Methods of a class can manipulate that class’s private data members that class’s private data members Classes provide public properties to Classes provide public properties to allow clients to set ( assign value allow clients to set ( assign value to) or get(obtain value of) private to) or get(obtain value of) private data members data members __property void set_Hour( int val) __property void set_Hour( int val) { { hour = (val >=0 &&val<24)?val:0); hour = (val >=0 &&val<24)?val:0); } } __property int get_Hour() __property int get_Hour() { { return hour; return hour; } }

description

properties. Methods of a class can manipulate that class’s private data members Classes provide public properties to allow clients to set ( assign value to) or get(obtain value of) private data members __property void set_Hour( int val) { hour = (val >=0 &&val

Transcript of properties

Page 1: properties

propertiesproperties Methods of a class can manipulate that class’s Methods of a class can manipulate that class’s

private data membersprivate data members Classes provide public properties to allow Classes provide public properties to allow

clients to set ( assign value to) or get(obtain clients to set ( assign value to) or get(obtain value of) private data membersvalue of) private data members

__property void set_Hour( int val)__property void set_Hour( int val) { {

hour = (val >=0 &&val<24)?val:0);hour = (val >=0 &&val<24)?val:0);

}} __property int get_Hour()__property int get_Hour() { { return hour;return hour; }}

Page 2: properties

propertiesproperties Providing set and get capabilities appears to be Providing set and get capabilities appears to be

the same as making the variables publicthe same as making the variables public Every property method name must be Every property method name must be

preceded by either set_ or get_preceded by either set_ or get_ __property void set_propertyName()__property void set_propertyName() PropertyName specifies the name of property.PropertyName specifies the name of property. Hour is scalar propertyHour is scalar property Scalar property is property that can be Scalar property is property that can be

accessed like variables. We assign value to accessed like variables. We assign value to scalar properties using = operator. When scalar properties using = operator. When assignment occurs, the code in the set assignment occurs, the code in the set accessor for that property executesaccessor for that property executes

Time *time = new Time();Time *time = new Time(); time->Hour = Int32::Parse(Console::ReadLine());time->Hour = Int32::Parse(Console::ReadLine());

Page 3: properties

Static membersStatic members Static variable: all class objects share only Static variable: all class objects share only

one copy of a static variable.one copy of a static variable. Static variable represents class-wide Static variable represents class-wide

informationinformation DeclarationDeclaration static in count;static in count; Static member can be initialized in its Static member can be initialized in its

declarationdeclaration Public static data can be accessed from client Public static data can be accessed from client

code through the class name using the scope code through the class name using the scope resolution operator::resolution operator::

The private static data can be accessed only The private static data can be accessed only through methods or property of the classthrough methods or property of the class

Page 4: properties

Static methodsStatic methods To enable a client to access a To enable a client to access a

private static member when no private static member when no objects of the class exists, the class objects of the class exists, the class must provide a public static method must provide a public static method or propertyor property

A static method cannot access non-A static method cannot access non-static datastatic data

Static variable and static methods Static variable and static methods exist even when there are no objects exist even when there are no objects of that class in which the static of that class in which the static members are definedmembers are defined

Page 5: properties

InheritanceInheritance

The previously defined class is The previously defined class is called the base classcalled the base class

New class is referred to as the New class is referred to as the derived classderived class

(other language, Java refer to (other language, Java refer to base as superclass and derived base as superclass and derived class as subclass)class as subclass)

Page 6: properties

Point classPoint class

public __gc class Pointpublic __gc class Point {{ public:public: Point();Point(); Point(int, int);Point(int, int); __property int get_X(){return x;}__property int get_X(){return x;} __property void set_X(int val){x = val;}__property void set_X(int val){x = val;} __property int get_Y(){ return y;}__property int get_Y(){ return y;} __property void set_Y(int val){ y = val;}__property void set_Y(int val){ y = val;} String *ToString();String *ToString(); private:private: int x,y;int x,y; };};

Page 7: properties

String * Point::ToString()String * Point::ToString() {{ return String::Concat(S”[“,x.ToString(), return String::Concat(S”[“,x.ToString(),

S”,”,y.ToString(), S”]”);S”,”,y.ToString(), S”]”); }} Point::Point(){ }Point::Point(){ } Point::Point( int xval, int yval)Point::Point( int xval, int yval) {{ x = xval;x = xval; y = yval;y = yval; }}

Page 8: properties

Circle – inherit Point classCircle – inherit Point class public __gc class Circle :public Pointpublic __gc class Circle :public Point {{ public:public: Circle();Circle(); Circle( int,int,double);Circle( int,int,double); __property double get_Radius(){return radius;}__property double get_Radius(){return radius;} __property void set_Radius(double r)__property void set_Radius(double r) {if( r > 0) radius = r;}{if( r > 0) radius = r;} double Diameter();double Diameter(); double Area();double Area(); double Circumference();double Circumference(); String *ToString();String *ToString(); private:private: double radius;double radius; };};

Page 9: properties

CircleCircle Circle::Circle(){}Circle::Circle(){} Circle::Circle(int xval,int yval, double r) :Point(xval,yval)Circle::Circle(int xval,int yval, double r) :Point(xval,yval) { radius = r;}{ radius = r;} double Circle::Diameter(){ return radius *2;}double Circle::Diameter(){ return radius *2;} }} double Circle::Circumference()double Circle::Circumference() {{ return Math::PI *Diameter();return Math::PI *Diameter(); }} double Circle::Area()double Circle::Area() { return Math::PI * Math::Pow(radius, 2);}{ return Math::PI * Math::Pow(radius, 2);} String *Circle::ToString()String *Circle::ToString() { return String::Concat(S”center= “, __super::ToString(), { return String::Concat(S”center= “, __super::ToString(),

S”;Radius = “, Radius.ToString());S”;Radius = “, Radius.ToString()); }}

Page 10: properties

int _tmain()int _tmain() {{ Circle *cir = new Circle(4,5,2.5);Circle *cir = new Circle(4,5,2.5); String *output = String::Concat(S”X String *output = String::Concat(S”X

coordinate is “, cir->X.ToString(), S”\coordinate is “, cir->X.ToString(), S”\nY coordinate is “, cir->Y.ToString(), nY coordinate is “, cir->Y.ToString(), S”\nRadius is “, cir-S”\nRadius is “, cir->Radius.ToString());>Radius.ToString());

Return 0;Return 0; }}

Page 11: properties

AssignmentAssignment Define a class called Cylinder Define a class called Cylinder

that inherits from Circle.that inherits from Circle. Has data member Has data member heightheight Methods: Methods: Volume()Volume() that return that return

volume of the cylindervolume of the cylinder Area() return the cylinder’s Area() return the cylinder’s

surface areasurface area Properties: set_Height and Properties: set_Height and

get_Heightget_Height

Page 12: properties

int _tmain()int _tmain(){{

Cylinder *cyn = new Cylinder(12,23,2.5,5.7);Cylinder *cyn = new Cylinder(12,23,2.5,5.7);String *out = String::Concat (S"X coordinate is ",String *out = String::Concat (S"X coordinate is ",

cyn->X.ToString(), S"\nY coordinate is ",cyn->X.ToString(), S"\nY coordinate is ",cyn->Y.ToString(),S"\nRadius is ",cyn->Y.ToString(),S"\nRadius is ",cyn->Radius.ToString(),S"\nHeight is ",cyn->Radius.ToString(),S"\nHeight is ",cyn->Height.ToString());cyn->Height.ToString());

out = String::Concat (out, S"\nDiameter is ",out = String::Concat (out, S"\nDiameter is ",cyn->Diameter().ToString(S"F"), S"\n");cyn->Diameter().ToString(S"F"), S"\n");

out = String::Concat (out, S"Volume is",out = String::Concat (out, S"Volume is",cyn->Volume().ToString(S"F"));cyn->Volume().ToString(S"F"));

MessageBox::Show (out, S"Demonstrating class MessageBox::Show (out, S"Demonstrating class Cylinder");Cylinder");return 0;return 0;

}}