Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 11 Introducing the Class Pages (245-271)

Similar presentations


Presentation on theme: "1 Chapter 11 Introducing the Class Pages (245-271)"— Presentation transcript:

1 1 Chapter 11 Introducing the Class Pages ( )

2 2

3 In OOD, the first step is to identify the components, called objects. ☼ An object combines data and operations on the data into a single unit. ☼ In C++, the mechanism that allow you to combine data and operations on the data into a single unit is called a class. ☼ A class is a collection of fixed number of components. The components of a class are called the members of the class. ☼ The general syntax for defining a class is: ☼ Class Members List consists of variable declarations and/or functions. ☼Note that class is a logical abstraction. It is not exist in memory until an object of that class has been created. class classIdentifier { classMembersList }; 3 Classes Overview

4 Example: class book { private: string author; string title; int status; public: void initial_book (string name, string thetitle, int s); int get_status(); void set_status(int s); void show(); }; 4

5 Void book::initial_book (string n, string t, int s)‏ { author = n; title = t; status = s; } int book::get_status()‏ { return status; } void book::set_status( int s)‏ { status = s; } 5

6 void book::show()‏ { cout << title << " by " << author; cout << " is "; if (status==1) cout << "in.\n"; else cout << "out.\n"; } 6

7 7 Class Members ☼Members of a class are classified into one of two categories: private and public. ☼ The private members of a class are not accessible outside the class. ☼ The public members of a class are accessible outside the class. ☼ By default, all members of a class are private. ☼The General syntax of classes is: ☼If any member of a class is a variable, it is declared like any other variable. ☼A class member is accessed using the class name, followed by the dot operator (.), followed by the member name. ☼A class function can be void or value returning function. Class class_name { Private data and function; Public: Public data and functions; };

8 Example: #include using namespace std; class Cat { int age;// 2 private variable by default int weight; public: // public is a keyword void setAge (int yrs) { age = yrs; }// void function (inline function) void setWeight (int kgs) { weight = kgs; } int getAge() { return age; }// value returning function int getWeight() { return weight; } }; 8

9 int main()‏ { Cat MyCat; // declare class variable (instance) MyCat.setAge(3); MyCat.setWeight(2); cout << "My cat\'s age is " << MyCat.getAge(); // call class function cout << " and weighs " << MyCat.getWeight() << " kg\n“ ; cout << endl; return 0; } 9

10 10 Constructors an Destructors ☼A constructor is a function that is called when an object is created. ☼Constructors guarantee that the member variables are initialized when an object is declared. ☼ The name of a constructor is the same as the name of the class. ☼ A class can have more than one constructor. ☼ A constructor without parameters is called the default constructor. ☼A destructor is a function that is called when an object is destroyed. ☼ Destructor automatically execute when a class object goes out of scope. ☼ A class can have only one destructor, and the destructor has no parameters.

11 11 Continuing ☼The name of a destructor is the tilde (~), followed by the class name (no spaces in between). ☼Constructors and Destructor are functions without any type; that is, they are neither value-returning nor void. As a result, they cannot be called like other functions.

12 Example: #include using namespace std; class Circle { private: float radius; public: Circle(); // default constructor Circle(float r); // parameterized constructor ~Circle(); // destructor void setRadius(float r); // public functions float getRadius(); float area(); float perimeter(); }; 12

13 // constructors Circle::Circle()‏ { radius=0; } Circle::Circle(float r)‏ { setRadius(r); } // destructor Circle::~Circle()‏ { cout<<" ending object "<=0)‏ radius=r; else radius=0; } 13

14 float Circle::getRadius()‏ { return radius; } float Circle::area()‏ { return 3.14*radius*radius; } float Circle::perimeter()‏ { return 2 * 3.14 * radius; } int main()‏ { float x; Circle c1; cout<<" Enter the radius of the circle: "; cin>>x; Circle C1(x); cout<<"\n the area of the circle is: "<

15 Inline Functions An inline function is a small function whose code is expanded in line rather than called. Example: class Cat { int age; int weight; public: void setAge (int yrs) { age = yrs; }// (inline function) void setWeight (int kgs) { weight = kgs; } int getAge() { return age; } int getWeight() { return weight; } }; 15

16 Example: #include using namespace std; class Rectangle { private: float length; float width; public: Rectangle(float L=1, float W=1); ~Rectangle(); void setLength(float L); void setWidth(float W); float getLength(); float getWidth(); float area(); float perimeter(); }; 16

17 Rectangle::Rectangle(float L, float W)‏ { setLength(L); setWidth(W); } Rectangle::~Rectangle()‏ { } void Rectangle::setLength(float L)‏ { if (L >=0)‏ length=L; else length=0; } void Rectangle::setWidth(float W)‏ { if(W >=0)‏ width=W; else width=0; } 17

18 float Rectangle::getLength()‏ { return length; } float Rectangle::getWidth()‏ { return width; } float Rectangle::area()‏ { return length * width; } float Rectangle::perimeter()‏ { return 2* ( length + width); } 18

19 int main ()‏ { float x,y; Rectangle R1, R2, R3; cout<<" Enter the length and width of the rectangle R1: "; cin>>x>>y; Rectangle R1(x,y),R2(12.0),R3(9.0,10.0); cout<<" the area of R1= "<


Download ppt "1 Chapter 11 Introducing the Class Pages (245-271)"

Similar presentations


Ads by Google