Lecture #6 Classes and Objects
Objects You can look around you now and see many examples of real-world objects: your cat, your desk, your television set, your bicycle. These real-world objects share two characteristics: they all have state and they all have behavior For example, dogs have state (name, color, breed, hungry) and dogs have behavior (barking, fetching).
Objects Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains its state in variables and implements its behavior with methods. An object combines data and operations on the data into a single unit.
Classes 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.
CLASSES class definition: classMembersList consists of variables (attribute) and/or functions (method) declarations . class classIdentifier { classMembersList };
Classes
Classes Members of a class are classified into one of three categories: Private (default) : not accessible outside the class. Protected: not accessible outside the class. Public: accessible outside the class. Note: The keyword of category name is followed by colon (:) . Usually the data within a class is private and the functions are public. In the definition of a class, you cannot initialize a variable when you declare it. In C++, a class is a definition. No memory is allocated for the class itself; memory is allocated for the class objects (class instances) when you declare them.
Example int main( ) { Person student; student.setAge(20); student.setName(“Nora”); cout << ”student\'s age is " << student.getAge(); cout << " and Name " << student.getName() << ” \n“ ; cout << endl; return 0; } #include <iostream> using namespace std; Class Person { private: int age; string name ; public: void setAge (int yrs) { age = yrs; } void setName (string N) { name = N; } int getAge() { return age; } string getName() { return N; } };
Classes Defining Objects: Calling Member Functions: Person student; student.setAge(20); student.setName(“Nora”);
Constructors Vs. Destructor Constructors guarantee that the member variables are initialized when an object is declared. Constructors automatically execute when a class 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, Constructor Overloading. A constructor without parameters is called the default constructor Destructor automatically execute when a class object goes out of scope. The name of a destructor is the tilde (~), followed by the class name (no spaces in between). A class can have only one destructor. The destructor has no parameters.
Constructors & Destructor Constructors and Destructor are functions without any type. As a result, they cannot be called like other functions. Note : A function can return a value of type class.
Example # 1 Circle::Circle(float r) { setRadius(r); } // destructor Circle::~Circle() {cout<<" ending object.."<<endl; void Circle::setRadius(float r) { if ( r >=0) radius=r; else radius=0; float Circle::getRadius() { return radius; float Circle::area() return 3.14*radius*radius; int main() { Circle C1(5); cout<<"\n the area of the circle is: "<<C1.area()<<end l; return 0; } #include<iostream> using namespace std; class Circle { private: float radius; public: Circle(); Circle(float r); ~Circle(); void setRadiu (float r); float getRadius(); float area(); }; // constructors Circle::Circle() { radius=0; }
Member Functions Defined Outside the Class void Circle::setRadius(float r) The function name, setRadius(), is preceded by the class name, Circle, and a new symbol the double colon :: the scope resolution operator. It is a way of saying, Circle::setRadius() means “the setRadius() is a member function of the Circle class.”
Example #2
Example#3 class country { private: int num_of_cities; public: country() {cout<<"\n Constructor called \n"; } void setNumOfCities(int num); int getNumOfCities(void); ~country() { cout<<"\n Destructor called \n"; } };
Con. Example#3 void country::setNumOfCities(int num) { num_of_cities = num; } int country::getNumOfCities(void) { return num_of_cities; } int main(void) { country Kuwait; int num = 5; Kuwait.setNumOfCities(num); num = Kuwait.getNumOfCities(); cout<<"\n Number of cities is equal to "<<num; return 0; }
Example #4 int main() { Book ABook(128) ; ABook.SetPage( 56 ); Cout << “The current page is: “ << ABook.GetCurrentPage() ; Return 0; } #include <iostream> #include <stdio.h> class Book { int PageCount; int CurrentPage; public: Book( int Numpages) ; // Constructor ~Book(){} ; // Destructor void SetPage( int PageNumber) ; int GetCurrentPage( void ) ; }; Book::Book( int NumPages) { PageCount = NumPages; } void Book::SetPage( int PageNumber) { CurrentPage=PageNumber; } int Book::GetCurrentPage( void ) { return CurrentPage; }