160 likes | 168 Views
This example demonstrates encapsulation and inheritance in object-oriented programming through a commission employee class. It also covers strings and formatting in C++/CLI.
E N D
outline • Class and Object-Oriented Programing • Encapsulation and inheritance • Example • Commission Employee • Encapsulation and inheritance • Strings and Formatting • In-class assignment • Derived class • Access scope
Class • Procedural vs Object-oriented • User-defined types (programmer-defined types) • class • Encapsulation (private and public) • Member Variables and access modifiers • Member Methods • Constructor • Constructor and method overloading • Method overloading: two or more methods have same name but with different number or type of parameters. • Using class
ref class Square { public: Square (int d) { Dims = d; } int Area () { return Dims*Dims; } int Dims; };
Use ref class • Square ^sqr1 = gcnew Square(); // a handle sqr1->Dims = 5; int area = sqr1->Area (); • Or: • Square sqr2; // a local or a stack instance sqr2.Dims = 3; int i = sqr2.Dims; int area = sqr2.Area ();
Example CommissionEmployee • firstName • lastName • socialSecurityNumber • grossSales • commissionRate • Read/write these variables • getXXX() / setXXX() • earnings() • print() private and public accesses
Constructor • Initialize the object at instantiate • A special method: called at object creation by gcnew(), same name as the class, no return (not defined with void either), can take any number of parameters, public • Constructor overloading class testclass { public: testclass () { x = y = 0; } testclass(Int32 a) { x = y = a; } // x =a; y = 10; testclass(Int32 a, Int32 b) { x = a; y = b; } private: Int32 x; Int32 y; };
testclass ^a1 = gcnew testclass(100); testclass ^a2 = gcnew testclass(); testclass ^a3 = gcnew testclass(100, 2);
static class methods and variables • Static variable in a class: • only one copy of the variable is created, and it is shared by all the instances of the class. (not associated with an instance) • Initiated only at the first time the class is instantiated. ref class staticVar { static int counts = 3; }; • Static method in a class: • the method is accessible without the need to instantiate the class. • Use only static member variables. Public: static int get_x () { return x; } • To use it: Console::WriteLine(className::get_x());
Inheritance • Code reuse • Base class and derived class in C++ • this and class scope(::) in C++ • Method overriding • A method in the derived ref class has an identical signature to the base ref class • Access modifiers • protected
using namespace System; // Base class ref class Square { public: int Area () { return Dims*Dims;} int Dims; }; // Derived class Ref class Cube : public Square { public: int Volume() { return Area()* Dims; } };
// inheritance in action void main(void) { Cube ^cube = gcnew Cube (); cube->Dims = 3; Console::WriteLine(cube->Dims); Console::WriteLine(cube->Area()); Console::WriteLine(cube->Volume()); }
Access modifiers • Public • Accessible by external functions and methods • Accessible to derived ref classes • Private • Not accessible by external functions and methods • Not accessible to derived ref classes • Protected • Not accessible by external functions and methods • Accessible to derived ref classes
Example • firstName • lastName • socialSecurityNumber • grossSales • commissionRate • earnings() • print() CommissionEmployee • baseSalary • earnings() • print() BasePlusCommissionEmployee
Strings and Formatting in C++/CLI • http://msdn.microsoft.com/en-us/library/system.string.aspx • public: static String^ Format( String^ format, ... array<Object^>^ args ) • Format:{index[,length][:formatString]} • {0}, {0, 10}, {0, -10}, {0, -10:D6}, {0,-10:f} double d = 134; int a = 30; Console::WriteLine("{0,-10:f}{1,-10}",d, a);
String^ dateString1 = "1"; String^ fmtString = String::Format("{0,10}",dateString1); // -10 to left, 10 to right Console::WriteLine(fmtString); int dateString2 = 1; String^ fmtString = String::Format("{0,-10:D6}",dateString2); Console::WriteLine(fmtString); String^ result = nullptr; result += String::Format("{0,10}", L"X4"); result += String::Format("{0, 10:D6}", dateString2); result += String::Format("{0,10}", L"end"); Console::WriteLine(result);
String^ result1 = L"X4"; String^ result2 = String::Format("{0} ", dateString2); String^ result3 = L"end"; Console::WriteLine("{0,10} {1, 10} {2,10}", result1, result2, result3); fmtString = String::Format("{0,10} {1, 10} {2,10}", result1, result2, result3); Console::WriteLine(fmtString); fmtString = String::Format("{0,10} {1, 10} {2,10}", result1, dateString1, result3); Console::WriteLine(fmtString);