struct to define a simple Circle 2 Examine the Circle code with struct and functions The C class syntax is built on the syntax of C struct Important concepts a A class is ID: 652558
Download Presentation The PPT/PDF document "First examine the code that uses a" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
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.
A First C++ Class – a
Circle
(code is available via link for today’s lecture)Slide2
Class Declaration
Syntax
class ClassName{ public: Declarations of public members (methods) private:
Declarations of private members (data)
};Slide3
Designing a Class
Data members normally placed in private: section of a class
Function members usually in public: sectionTypically public: section followed by privatealthough not required by compilerDiscuss new documentation requirements Class documentation – what it does Precondition – what must be true for method to execute correctly Postcondition – expected results if precondition satisfiedSlide4
Class Libraries
Class declarations placed in header file
Given .h extensionContains data items and prototypesImplementation fileSame prefix name as header fileGiven .cpp extensionPrograms which use this class library are called client programs.Slide5
Example of User-Defined Circle Class
See Circle.h and Circle.cpp1.
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 satisfiedSlide6
ConstructorsNote constructor definitions
in Circle.cpp Syntax for default constructor
ClassName::ClassName (): member_initializer_list{ // body of constructor definition}Syntax for parameterized constructorClassName::ClassName (parameter_list){ // body of constructor definition}
6Slide7
ConstructorsResults of default constructor
?Circle c;Results of parameterized
constructor?Circle bigC(0, 0, 500);7Slide8
Overloading FunctionsNote existence of multiple functions with the same name
1. Circle();
2.Circle(float x, float y, float r);Known as overloadingCompiler compares numbers and types of arguments of overloaded functionsChecks the "signature" of the functions1. – default constructor2. – parameterized constructor8Slide9
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 objectsSyntax for prototype of parameterized constructor with default valuesClassName::ClassName (type1 parm1 = val
1
, …,
type
n
parm
n
=
val
n
)
;
Syntax for implementation of parameterized
constructor
ClassName
(type
1
parm1, …, typen parmn){ // body of constructor definition}
9Slide10
Copy OperationsDuring initialization
Circle c = c1;
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.10Slide11
Other Class OperationsAccessors and Mutators
See “get" and ”
set" functionsOverloading operatorsSame symbol can be used more than one wayNote declaration for I/O operators << and >>Note definition of overloaded I/O operatorsfriend ostream& operator<<(ostream & out, const Circle &c); friend istream& operator>>(istream & in, Circle &c);11Slide12
Friend FunctionsPossible to specify operator<<() as a "friend" function
Thus given "permission" to access private data elementsDeclaration 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.)Slide13
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 functionnot qualified with class name and ::receives class object on which it operates as a parameterSlide14
Other OperationsRelational Operators
Circle object compares itself with anotherDetermines if it is less than the other
bool Circle::operator<(const Circle &circle1) const{ return radius < circle1.radius;}Slide15
Redundant DeclarationsNote use of #include
”SimpleCircle.h" inSimpleCircle.cppClient
main programCauses "redeclaration" errors at compile timeSolution is to use conditional compilationUse #ifndef and #define and #endif compiler directivesSlide16
Pointers to Class ObjectsPossible to declare pointers to class objects
Circle * cPtr
= &c;Access with cPtr->getX (); or (*cPtr).getX();Slide17
The this Pointer
Every class has a keyword, thisa pointer whose value is the address of the objectValue of *this
would be the object itselfvoid Circle::setX(float x){ this->xCenter = x;}Slide18