CHAPTER 5 Introduction to Classes and Objects Review

CHAPTER 5 Introduction to Classes and Objects

Review: Two Programming Paradigms Structural (Procedural) PROGRAM Object-Oriented PROGRAM OBJECT FUNCTION Operations FUNCTION Function calls Data OBJECT Operations Data Messages passing 2

Review: C++ Data Types simple integral enum structured floating array struct union class char short int long bool float double long double address pointer reference 3

Classes & Objects • The class is the cornerstone of C++ – It gives the C++ its identity from C – It makes possible encapsulation, data hiding and inheritance • Class: – Consists of both data and methods – Defines properties and behavior of a set of entities • Object: – An instance of a class – A variable identified by a unique name 4

Define a Class Type Header class_name { permission_label: member; Body permission_label: member; . . . }; class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; 6


Defining a member function with a parameter #include <iostream. h> // member function definitions class circle { private: double radius; void circle: : store(double r) { radius = r; } double circle: : area(void) { return 3. 14*radius; } void circle: : display(void) { cout << “r = “ << radius << endl; } public: void store(double); double area(void); void display(void); }; int main(void) { circle c; // an object of circle class c. store(5. 0); cout << "The area of circle c is " << c. area() << endl; c. display(); } 8

Constructors • Constructor: – a function used to initialize the data of an object of a class – Same name as class itself – Cannot return anything, not even void – A class may define more than one constructor • With different parameter lists • Default constructor has no parameters • Called automatically – When class object is declared as automatic variable – By new operator 9

Constructor Example Constructor has same name as class and no return type Initialize data member

Constructor Example ct e j b ss o emory a l c this ated m o s d oc e l l e a n y tor micall c u r t Des ee dyna r can f

Constructor Example Creating objects implicitly calls the constructor

Defining the constructor class Circle { private: double radius; public: Circle(); Circle(int r); void set. Radius(double r); double get. Diameter(); double get. Area(); double get. Circumference(); }; Constructor with no argument – Default Constructor with one argument

Destructors • Destructor: – a function used to clean up an object of a class prior to deleting that object – Class name preceeded by '~' – No parameters, no result • Called automatically – When function exits scope of automatic class object – By delete operator 14

Destructors Example class string { private: char *s; int size; public: string(char *); ~string(); }; // constructor // destructor string: : string(char *c) { size = strlen(c); s = new char[size+1]; strcpy(s, c); } string: : ~string() { delete []s; }

Composition: Objects as member of classes OBJECT Operations Data What is an object? set of methods (public member functions) internal state (values of private data members) 16

Declaration of an Object class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } main() { Rectangle r 1; Rectangle r 2; r 1. set(5, 8); cout<<r 1. area()<<endl; } r 2. set(8, 10); cout<<r 2. area()<<endl; 17

Example #include <iostream. h> // member function definitions class circle { private: double radius; void circle: : store(double r) { radius = r; } double circle: : area(void) { return 3. 14*radius; } void circle: : display(void) { cout << “r = “ << radius << endl; } public: void store(double); double area(void); void display(void); }; int main(void) { circle c; // an object of circle class c. store(5. 0); cout << "The area of circle c is " << c. area() << endl; c. display(); } 18
- Slides: 17