1 / 19

C++ Review

C++ Review. User Defined Types. Built-in data types are simple. Specific problems may demand aggregate/more complex data types. Ex: polygons, matrices, car specifications, etc… Types define both data and operations that can be performed on that data

ora
Télécharger la présentation

C++ Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Review

  2. User Defined Types • Built-in data types are simple. Specific problems may demand aggregate/more complex data types. • Ex: polygons, matrices, car specifications, etc… • Types define both data and operations that can be performed on that data • Ex: vectors might store x,y coordinates and have addition/subtraction operations • C++ allows for complex types in the form of classes and structs

  3. Class Example structPoint { floatm_x, m_y; void Set ( float x, float y ) { m_x = x; m_y = y; } }; class Point { floatm_x, m_y; public: void Set ( float x, float y ) { m_x = x; m_y = y; } }; In C++, the only difference between struct/class is public/private scope

  4. Public, Protected, Private Scopes • Public: functions/data that any part of a program may access • Ex: Data Accessors – GetData(), modifiers – SetData(newData), operations – ActionX() • Protected: functions/data that only the class and any derived subclasses may access • Ex: Helper functions for operations. Most data in class hierarchies • Private: functions/data that only the class itself may access • Ex: Helper functions and some data

  5. Encapsulation • A mechanism for restricting access to some of the object's components • Advice: • Helper functions and data should be “Encapsulated”, i.e., should be private or protected • Use Accessors/Modifiers to “Get”/“Set” the data

  6. Constructors and Destructors • Constructors – “functions” that define how data is initialized • Same name as the class itself • Default constructor – takes no parameters, e.g., Point() • Copy constructor – takes an object of the class itself to initialize the data, e.g., Point(oldPoint) • Destructors – define how data is deleted • Same name as the class except with a ~, e.g., ~MyClass() • Usually not called in code you write. The compiler automatically calls it when an object is deleted.

  7. Abstract Types and Hierarchies • Often many types have things in common, e.g., all shapes can be drawn or all shapes have color • However, you cannot concretely define a shape. In this case a shape is abstract • We derive more concrete classes off of abstract ones, e.g., a circle is a concrete shape

  8. Inheritance • Inheritance allows reuse of code in existing objects and definition of subtypes Shape Circle Square Triangle

  9. Inheritance • Inheritance allows reuse of code in existing objects and definition of subtypes Base class: class that is inherited from Shape Circle Square Triangle

  10. Inheritance • Inheritance allows reuse of code in existing objects and definition of subtypes Derived class: class that does the inheriting Shape Circle Square Triangle

  11. Inheritance • Inheritance allows reuse of code in existing objects and definition of subtypes • Derived classes have access to public/protected data and functions of the base class Derived class: class that does the inheriting Shape Circle Square Triangle

  12. Inheritance Example class Shape { protected: Color m_c; public: Shape(Color c) : m_c(c) {} void Draw() {/*do nothing*/} }; class Circle : public Shape { floatm_radius; public: Circle(Color c, float r) : Shape(c), m_radius(r) {} void Draw() {/*draw circle of radius r*/} }; class Square : public Shape { floatm_side; public: Square(Color c, float s) : Shape(c), m_side(s) {} void Draw() {/*draw circle of radius r*/} };

  13. Virtual Functions • Function whose behavior can be overridden in a derived class. • A call to a virtual function will call the function of the original object regardless of the type of the pointer. class Shape { protected: Color m_c; public: Shape(Color c) : m_c(c) {} // pure virtual function virtualvoid Draw() = 0; }; class Circle : public Shape { floatm_radius; public: Circle(Color c, float r) : Shape(c), m_radius(r) {} virtualvoid Draw() {/*draw circle of radius r*/} };

  14. Virtual Functions • Function whose behavior can be overridden in a derived class. • A call to a virtual function will call the function of the original object regardless of the type of the pointer. class Shape { protected: Color m_c; public: Shape(Color c) : m_c(c) {} // pure virtual function virtualvoid Draw() = 0; }; class Circle : public Shape { floatm_radius; public: Circle(Color c, float r) : Shape(c), m_radius(r) {} virtualvoid Draw() {/*draw circle of radius r*/} }; Requires derived classes to implement this function.

  15. Virtual Functions • Function whose behavior can be overridden in a derived class. • A call to a virtual function will call the function of the original object regardless of the type of the pointer. class Shape { protected: Color m_c; public: Shape(Color c) : m_c(c) {} // pure virtual function virtualvoid Draw() = 0; }; class Circle : public Shape { floatm_radius; public: Circle(Color c, float r) : Shape(c), m_radius(r) {} virtualvoid Draw() {/*draw circle of radius r*/} }; Classes with pure virtual functions are abstract and cannot be instantiated.

  16. Virtual Functions • Function whose behavior can be overridden in a derived class. • A call to a virtual function will call the function of the original object regardless of the type of the pointer. class Shape { protected: Color m_c; public: Shape(Color c) : m_c(c) {} // pure virtual function virtualvoid Draw() = 0; }; class Circle : public Shape { floatm_radius; public: Circle(Color c, float r) : Shape(c), m_radius(r) {} virtualvoid Draw() {/*draw circle of radius r*/} }; Shape *s = new Circle ( Red, 1.0f ); s->Draw ( ); // calls Circle:Draw ( )

  17. Virtual Functions • Function whose behavior can be overridden in a derived class. • A call to a virtual function will call the function of the original object regardless of the type of the pointer. class Shape { protected: Color m_c; public: Shape(Color c) : m_c(c) {} void Draw() {/*do nothing*/} }; class Circle : public Shape { floatm_radius; public: Circle(Color c, float r) : Shape(c), m_radius(r) {} virtualvoid Draw() {/*draw circle of radius r*/} }; Shape *s = new Circle ( Red, 1.0f ); s->Draw ( ); // calls Shape:Draw ( )

  18. Operator Overloading • Operator overloading provides a way to use common operators with user defined types • Ex: assignment (=), addition (+), input (>>) classMyClass{ private: string m_name; public: MyClass(string name) : m_name(name) {} MyClass& operator=(constMyClass& m) { m_name = m.m_name; } };

  19. Exercises • Create a struct called Point. This will be a 2D point. Provide a constructor, overloaded assignment operator, and public data.  • Create an abstract class called Shape. Shape will have protected data of a Point describing the location of the shape and a pure virtual function Print(). • Create subclasses of Shape, Circle and Square with private data. Overload the print function to print out the type and data members of the class. For example, Circle::Print () might output “Circle at position 3,5 with radius 2”. • Create a main function instantiating a Square and a Circle as an abstract Shape. Call the virtual function of Shape.

More Related