A First C++ Class – a Circle First examine the code that uses a struct to define a simple Circle. The C++ class syntax is built on the syntax of C struct. Important concepts: A class is an encapsulation of state (data members) and behavior (methods). Encapsulation supports information hiding, abstraction, modularity, and enhances security.
Class Declaration Syntax class ClassName { public: Declarations of public members (methods) private: Declarations of private members (data) };
Designing a Class Data members normally placed in private: section of a class Function members usually in public: section Typically public: section followed by private: although not required by compiler
Class Libraries Class declarations placed in header file Given .h extension Contains data items and prototypes Implementation file Same prefix name as header file Given .cpp extension Programs which use this class library called client programs
Example of User-Defined Circle Class See Circle.h and Circle.cpp Circle.h Constructor prototype – use name of class with no return type (builds the object) Note use of initializer list in default constructor Initializer list not used in parameterized constructor because we CHECK the values passed by the client to ensure precondition is satisfied
Constructors Note constructor definitions in Circle.cpp Syntax for default constructor ClassName::ClassName (parameter_list) : member_initializer_list { // body of constructor definition } Syntax for parameterized constructor ClassName::ClassName (parameter_list) { // body of constructor definition }
Constructors Results of default constructor? Circle c; Results of parameterized constructor? Circle bigC(0, 0, 500);
Overloading Functions Note existence of multiple functions with the same name 1. Circle(); 2.Circle(float x, float y, float r); Known as overloading Compiler compares numbers and types of arguments of overloaded functions Checks the "signature" of the functions 1. – default constructor 2. – parameterized constructor
Default Arguments Possible to specify default values for constructor arguments – allows any reasonable construction of an object Circle (float x = 2.0, float y = 2.0, float r = 10); Consider? Circle c1, c2(5), c3(5,30), c4(5,30,120); This allows the class designer to supply one constructor that gives the client reasonable options for constructing objects Syntax for parameterized constructor with default values ClassName::ClassName (type1 parm1 = val1, …, typen parmn = valn) { // body of constructor definition }
Copy Operations During initialization Circle c = c1; During Assignment c = c1; When passing by value or returning a value from a function/method We can do this because all the class data members are primitive types that the compiler knows how to copy. When the class data members are aggregate types a copy constructor and assignment operator must be added to the class definition.
Other Class Operations Accessors and Mutators See “get" and ”set" functions Overloading operators Same symbol can be used more than one way Note declaration for I/O operators<< and >> Note definition of overloaded I/O operators friend ostream& operator<<(ostream & out, const Circle &c); friend istream& operator>>(istream & in, Circle &c);
Friend Functions Possible to specify operator<<() as a "friend" function Thus given "permission" to access private data elements Declaration in .h file (a friend function is not a class member – it may be a free function or it may be a method belonging to another class. Operator<< is a method of the ostream class.)
Friend Functions Definition in .cpp file (remove friend reserved word here) ostream& operator<<(ostream & out, const Circle &c) { out<< " Circle radius = " << c.radius << endl; out << " Circle is centered at " << c.xCenter << "," << c. yCenter << endl; return out; } Note - a friend function is not a member function not qualified with class name and :: receives class object on which it operates as a parameter
Other Operations Relational Operators Circle object compares itself with another Determines if it is less than the other bool Circle::operator<(const Circle &circle1) const { return radius < circle1.radius; }
Redundant Declarations Note use of #include ”SimpleCircle.h" in SimpleCircle.cpp Client main program Causes "redeclaration" errors at compile time Solution is to use conditional compilation Use #ifndef and #define and #endif compiler directives
Pointers to Class Objects Possible to declare pointers to class objects Circle * cPtr = &c; Access with cPtr->getX (); or (*cPtr).getX();
The this Pointer Every class has a keyword, this a pointer whose value is the address of the object Value of *this would be the object itself void Circle::setX(float x) { this->xCenter = x; }