A First C Class a Circle code is

  • Slides: 18
Download presentation
A First C++ Class – a Circle (code is available via link for today’s

A First C++ Class – a Circle (code is available via link for today’s lecture) 1. First examine the code that uses a struct to define a simple Circle. 2. Examine the Circle code with struct and functions. Discuss reference parameters (&) and operator functions (operator overloading). The C++ class syntax is built on the syntax of C struct. Important concepts: A class is a data type that encapsulates state (data members) and behavior (methods). Encapsulation supports information hiding, abstraction, modularity, and enhances security.

Class Declaration • Syntax class Class. Name { public: Declarations of public members (methods)

Class Declaration • Syntax class Class. Name { 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

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 Discuss new documentation requirements Class documentation – what it does Precondition – what must be true for method to execute correctly Postcondition – expected results if precondition satisfied

Class Libraries Class declarations placed in header file Given. h extension Contains data items

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 are called client programs.

Example of User-Defined Circle Class See Circle. h and Circle. cpp 1. Circle. h

Example of User-Defined Circle Class See Circle. h and Circle. cpp 1. Circle. h Constructor prototype – use name of class with no return type (builds/initializes 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 Class. Name: :

Constructors Note constructor definitions in Circle. cpp Syntax for default constructor Class. Name: : Class. Name () : member_initializer_list { // body of constructor definition } • Syntax for parameterized constructor Class. Name: : Class. Name (parameter_list) { // body of constructor definition } 6

Constructors • Results of default constructor? Circle c; • Results of parameterized constructor? Circle

Constructors • Results of default constructor? Circle c; • Results of parameterized constructor? Circle big. C(0, 0, 500); 7

Overloading Functions • Note existence of multiple functions with the same name 1. Circle();

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 8

Default Arguments Possible to specify default values for constructor arguments – allows any reasonable

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 c 1, c 2(5), c 3(5, 30), c 4(5, 30, 120); This allows the class designer to supply one constructor that gives the client reasonable options for constructing objects Syntax for prototype of parameterized constructor with default values Class. Name: : Class. Name (type 1 parm 1 = val 1, …, typen parmn = valn); Syntax for implementation of parameterized constructor Class. Name (type 1 parm 1, …, typen parmn) { // body of constructor definition } 9

Copy Operations During initialization Circle c = c 1; When passing by value or

Copy Operations During initialization Circle c = c 1; When passing by value or returning a value from a function/method Circle foo (Circle c); 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. More on this later. 10

Other Class Operations • Accessors and Mutators • See “get

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); 11

Friend Functions Possible to specify operator<<() as a

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 &

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. x. Center << ", " << c. y. Center << 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

Other Operations Relational Operators • Circle object compares itself with another • Determines if it is less than the other bool Circle: : operator<(const Circle &circle 1) const { return radius < circle 1. radius; }

Redundant Declarations Note use of #include ”Simple. Circle. h

Redundant Declarations Note use of #include ”Simple. Circle. h" in • Simple. Circle. 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 * c.

Pointers to Class Objects Possible to declare pointers to class objects Circle * c. Ptr = &c; Access with c. Ptr->get. X (); or (*c. Ptr). get. X();

The this Pointer Every class has a keyword, this • a pointer whose value

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: : set. X(float x) { this->x. Center = x; }