Download presentation
Presentation is loading. Please wait.
Published byArchibald Price Modified over 8 years ago
1
1 C++ Programming Classes ● Class definition and implementation ● Constructors, data members ● Member Functions ● Structuring C + + code
2
2 Introduction ● We will demonstrate the development of C++ code on a simple class representing a point in 2D space ● We will start from the requirements that our class should fulfil: – Point can be created from its x,y coordinates – It can calculate the distance to another point – It can return its coordinates x and y ● Class definition and implementation – In the class definition, we provide only the declaration of member functions and data members ● Like a function declaration, the class definition is a contract which defines the functions the class must take – The class member functions implementations (definitions) are usually provided in a separate file, called also the class implementation
3
3 Class Definition Class definition: class class-name { class-body }; The body of the class (class-body) consists of declarations of member functions and data members of the class: – The functions used to manipulate the object from outside are in the "public" area – The members are in the "private" area (Reminder: encapsulation) – In the "private" area one can also find functions that are accessible only by the class itself class class-name { public: // Function used to construct the object // Function to handle the object // Function to access the state of the object private: // Data members };
4
4 The definition of class Point class Point { public: // Functions used to construct the object Point(float x, float y); Point(); // Functions to handle the object float Distance(Point point); // Functions to access the state of the object float GetX(); float GetY(); private: // Data members float m_x; float m_y; }; class class-name { public: // Functions used to construct the object // Functions to handle the object // Functions to access the state of the object private: // Data members }; ● The choice of member functions is driven by the requirements: – Point can be created from its x,y coordinates – It can calculate the distance to another point – It can return its coordinates x and y File Point.h
5
5 Class Implementation Member Functions The definition of member functions to manipulate the object: return-type class-name::function-name { function-body } The functions of the class Point: float Point::Distance(Point point) { /// Calculate the distance to a point float x_diff = m_x – point.m_x; float y_diff = m_y – point.m_y; return sqrt(x_diff*x_diff + y_diff*y_diff); } float Point::GetX() { return m_x; } float Point::GetY() { return m_y; } In file Point.cxx float Distance(Point point) Point(); float GetX(); float GetY() ; In file Point.h
6
6 Constructors The constructor of the class Point from coordinates x, y: Point::Point(float x, float y) : m_x(x), m_y(y) { /// The constructor of the class Point from /// coordinates x, y } class-name::class-name(arguments-list) : data-member-initialisation { function-body } ● The constructor is a special member function that is used to construct the object ● The initialization list (data-member- initialization) includes all the members of the class, following the same order as their declaration in the class definition – It is not mandatory, but if it is not done, it is done by the compiler using random values ● A class may have several constructors In file Point.cxx Point(float x, float y); In file Point.h
7
7 The Default Constructor The default constructor is a special constructor which has no arguments: The default constructor of the class Point: class-name::class-name() : initialisation-list { function-body } Point::Point() : m_x(0), m_y(0) { /// The default constructor /// (The point is initialized to the center) } – The data members are initialized to their default values In file Point.cxx Point(); In file Point.h
8
8 Using classes #include “Point.h” int main() { // Create two points Point point1; Point point2(2, 3); // Calculate and print their distance std::cout << point1.Distance(point2) << std::endl;... } ● The following code can appear in the main() function of the program or in another class In file main.cxx
9
9 C++ Code Structuring... ● The code is usually structured by classes ● The definition of the class is separated from the implementation of its methods, so we have two files per class – Header file – extension.h (.hh) – Implementation file – extension:.cxx (.cpp,.cc,.C,...) ● The header file is sometimes called the class interface, what should not be confused with the same term used for abstract classes class Point { public: Point(float x, float y); float Distance(Point point); private: float m_x; float m_y; }; #include “Point.h” Point::Point(float x, float y) : m_x(x), m_y(y) {} float Point::Distance(Point point) { float x_diff = m_x – point.m_x; float y_diff = m_y – point.m_y; return sqrt(x_diff*x_diff + (x_diff*x_diff); } #include “Point.h” #include int main() { Point p1(0, 0); Point p2(2, 3); std::cout << p1.Distance(p2) << std::endl; return 0; } Point. h Point.cx x main.cx x
10
10... C++ Code Structuring ● Classes can be structured in packages, the files are then placed in individual directories per package – Often the directory is divided into 2 sub-directories: include (*.h), src (*.cxx) ● The design must deal with relationships (dependencies) between packages – We must define the package hierarchy – Avoid circular dependencies ● The coding style can vary from one project to another but the practice is to adopt and to follow it consistently: – Place the public space before the private – Data members begin with m_ – Function members begin with a capital letter –...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.