1 / 12

properties

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<24)?val:0); }

Download Presentation

properties

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. 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<24)?val:0); • } • __property int get_Hour() • { • return hour; • }

  2. properties • Providing set and get capabilities appears to be the same as making the variables public • Every property method name must be preceded by either set_ or get_ • __property void set_propertyName() • PropertyName specifies the name of property. • Hour is scalar property • Scalar property is property that can be accessed like variables. We assign value to scalar properties using = operator. When assignment occurs, the code in the set accessor for that property executes • Time *time = new Time(); • time->Hour = Int32::Parse(Console::ReadLine());

  3. Static members • Static variable: all class objects share only one copy of a static variable. • Static variable represents class-wide information • Declaration • static in count; • Static member can be initialized in its declaration • Public static data can be accessed from client code through the class name using the scope resolution operator:: • The private static data can be accessed only through methods or property of the class

  4. Static methods • To enable a client to access a private static member when no objects of the class exists, the class must provide a public static method or property • A static method cannot access non-static data • Static variable and static methods exist even when there are no objects of that class in which the static members are defined

  5. Inheritance • The previously defined class is called the base class • New class is referred to as the derived class • (other language, Java refer to base as superclass and derived class as subclass)

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

  7. String * Point::ToString() • { • return String::Concat(S”[“,x.ToString(), S”,”,y.ToString(), S”]”); • } • Point::Point(){ } • Point::Point( int xval, int yval) • { • x = xval; • y = yval; • }

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

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

  10. int _tmain() • { • Circle *cir = new Circle(4,5,2.5); • String *output = String::Concat(S”X coordinate is “, cir->X.ToString(), S”\nY coordinate is “, cir->Y.ToString(), S”\nRadius is “, cir->Radius.ToString()); • Return 0; • }

  11. Assignment • Define a class called Cylinder that inherits from Circle. • Has data member height • Methods: Volume() that return volume of the cylinder • Area() return the cylinder’s surface area • Properties: set_Height and get_Height

  12. int _tmain() { Cylinder *cyn = new Cylinder(12,23,2.5,5.7); String *out = String::Concat (S"X coordinate is ", cyn->X.ToString(), S"\nY coordinate is ", cyn->Y.ToString(),S"\nRadius is ", cyn->Radius.ToString(),S"\nHeight is ", cyn->Height.ToString()); out = String::Concat (out, S"\nDiameter is ", cyn->Diameter().ToString(S"F"), S"\n"); out = String::Concat (out, S"Volume is", cyn->Volume().ToString(S"F")); MessageBox::Show (out, S"Demonstrating class Cylinder"); return 0; }

More Related