Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Classes and Objects

Similar presentations


Presentation on theme: "Introduction to Classes and Objects"— Presentation transcript:

1 Introduction to Classes and Objects
CHAPTER 5 Introduction to Classes and Objects

2 Review: Two Programming Paradigms
Structural (Procedural) Object-Oriented PROGRAM PROGRAM OBJECT Operations Data FUNCTION FUNCTION OBJECT Operations Data A list of tasks to perform. Viewed as a collection of interacting objects. Each object can be viewed as an independent machine with a distinct role or responsibility. Operations are closely associated with the objects, carry their own operators around with them . OBJECT Operations Data FUNCTION Function calls Messages passing

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

4 Classes & Objects The class is the cornerstone of C++ Class: Object:
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 Aim of class: a) provide the programmer with a tool for creating new types that can be used as conveniently as the built-in types (like float) b) user-defined type why? Separate the incidental details of the implementation from the properties essential to the correct use of it 2. Derived class, templates: organizing related classes that allow the programmer to take advantage of their relationships.

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

6

7 Defining a member function with a parameter
#include <iostream.h> class circle { private: double radius; public: void store(double); double area(void); void display(void); }; // member function definitions void circle::store(double r) { radius = r; } double circle::area(void) return 3.14*radius*radius; void circle::display(void) cout << “r = “ << radius << endl; 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

10 Constructor Example Destructor need so this class object can free dynamically allocated memory

11 Creating objects implicitly calls the constructor
Constructor Example Creating objects implicitly calls the constructor

12 Defining the constructor
class Circle { private: double radius; public: Circle(); Circle(int r); void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; Constructor with no argument – Default Constructor Constructor with one argument

13 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 *); // constructor ~string(); // destructor }; string::string(char *c) { size = strlen(c); s = new char[size+1]; strcpy(s,c); } string::~string() delete []s;

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

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

17 Example #include <iostream.h> // member function definitions
class circle { private: double radius; public: void store(double); double area(void); void display(void); }; // member function definitions void circle::store(double r) { radius = r; } double circle::area(void) return 3.14*radius*radius; void circle::display(void) cout << “r = “ << radius << endl; 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(); }


Download ppt "Introduction to Classes and Objects"

Similar presentations


Ads by Google