Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Programming
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014
2
Classes and Objects ►Class is a new data type which is used to define objects. A class serves as a plan, or a template. It specifies what data and what functions will be included in objects of that class. Writing a class doesn’t create any objects. ► A class is a description of similar objects. ► Objects are instances of classes.
3
Example A model (class) to define points in a graphics program. ►Points on a plane must have two properties (states): – x and y coordinates. We can use two integer variables to represent these properties. ►In our program, points should have the following abilities (behavior): – Points can move on the plane: move function – Points can show their coordinates on the screen: print function – Points can answer the question whether they are on the zero point (0,0) or not: is_zero function
4
Class Definition : Point
class Point { // Declaration of Point Class int x,y; public: void move(int, int); void print(); bool is_zero(); }; Data and functions in a class are called members of the class. In our example only the prototypes of the functions are written in the class declaration. Every element following public:keyword is accessible from outside of class If the body of a function is written in the class declaration, then this function is defined as an inline function (macro). Classes are same as structures BUT members of classes are private by default, but members of Structures are public.
5
Bodies of member function
// A function to move the points void Point::move (int new_x, int new_y) { x = new_x; // assigns new value to x coordinate y = new_y; // assigns new value to y coordinate } Now we have a model (template) to define point objects. We can create necessary points (objects) using the model. int main() { Point point1; //declare object or class point point1; point1.move(100,50); // point1 moves to (100,50) Return 0;
6
Example Class Diagram ► A class is a grouping of data and
functions .used to create a variable which can be manipulated in a program. ► An object is an instance of a class, which is similar to a variable defined as an instance of a type. An object is what you actually use in a program.
7
Inline Function A normal function is placed in a separate section of code and a call to the function generates a jump to this section of code. In Inline Function: A copy of the function statements is placed directly into the compiled calling program. Less overhead. Used when: » The number of statements is small (one or two lines in the body of the function) » The function is called few times
8
Inline Function…Cont. Or Two way to write inline function:
class Point { int x,y; public: void print(){ cout<<x <<“ ” <<y}; //inline function }; Or Inline void print(); //inline function Void print ::Point{ cout<<x <<“ ” <<y }
9
Controlling Accesses to Members
We can divide programmers into two groups: class creators (those who create new data types) and client programmers (the class consumers who use the data types in their applications) The goal of the class creator is to build a class that includes all necessary properties and abilities. The class should expose only what’s necessary to the client programmer and keeps everything else hidden The hidden parts are only necessary for the internal machinations of the data type but not part of the interface that users need in order to solve their particular problems The class creator can change the hidden portion at will without worrying about the impact to anyone else.
10
Controlling Accesses to Members
public: , private and protected are used to control access to a class' data members and functions. Private class members can be accessed only by members of that class. Public members may be accessed by any function in the program. The default access mode for classes is private
11
Constructors The class designer can guarantee initialization of every object by providing a special member function called the constructor. The constructor is invoked automatically each time an object of that class is created (instantiated). These functions are used to (for example): assign initial values to the data members open files establish connection to a remote computer etc. The constructor can take parameters as needed, but it cannot have a return value The constructor has the same name as the class itself.
12
Defaults constructor Defaults constructor: a constructor that can be invoked with no arguments Example class Point{ int x,y; public: Point(); // Declaration of the default constructor bool move(int, int); void print(); }; Point::Point() { // Default Constructor cout << "Constructor is called..." << endl; x = 0; y = 0; }
13
Example :Constructor with Parameters
class Point{ int x,y; public: Point(int,int); bool move(int, int); void print(); }; Point::Point(int x_first, int y_first) { if ( x_first < 0 ) x = 0; else x = x_first; if ( y_first < 0 ) y = 0; // Assigns zero to x y = y_first; } int main() { Point p1(20, 100); // Construct is called 2 times Point p3; // ERROR! There is not a default constructor :
14
Constructor Parameters with default value
Example class Point{ int x,y; public: Point(int x_first = 0, int y_first = 0); bool move(int, int); void print(); }; Point::Point(int x_first, int y_first) { if ( x_first < 0 ) x = 0; else x = x_first; if ( y_first < 0 ) y = 0; // Assigns zero to x y = y_first; } int main() { Point p1(20, 100); // Construct is called Point p3; // x=0, y=0 :
15
Copy Constructor It is a special type of constructors and used to copy the contents of an object to a new object during construction of that new object. The type of its input parameter is a reference to the object that will be copied into the new object. Example #include<iostream.h> class point{ public: int x,y; point(const point &c):x(c.x), y(c.y){} point():x(0), y(0){} print(){cout<<x<<" "<<y;} }; int main() { point p1; p1.x=20; p1.y=50; p1.print(); point p2(p1); // or point p2=p1; p2.print(); }
16
Multiple Constructors
Class may have more than one constructor with different type of input parameters. Example Point::Point() { // Default constructor // Body is not important } Point::Point(int x_first, int y_first) { // A constructor with parameters // Body is not important The client programmer can define objects in different ways int main() { Point p1(20, 100); // Construct is called Point p3; Point p3(10); //ERROR! There isn't a constructor with one parameter :
17
Destructors The destructor is very similar to the constructor except that it is called automatically when a dynamic object is deleted from memory by using the delete operator. A destructor is characterized as having the same name as the class but with a tilde ‘~’ preceded to the class name. A destructor has no return type and receives no parameters. A class may have only one destructor.
18
Example: Destructors Example class Point{ int x,y; public:
Point(int x_first = 0, int y_first = 0); bool move(int, int); void print(); ~Point(); }; Point::~Point() { cout << "Destructor has been invoked" << endl; }
19
Static Class Member Normally, each object of a class has its own copy of all data members of the class. Static data :one copy of a particular data member should be shared by all objects of a class Static data must be initialized once (and only once) at file scope and it can be altered Constant data cannot be changed but a copy is made for every object. class AA{ char c; static int i; }; int main() { AA p, q, r; : }
20
Example double Student::RegistrationFee = 50.5;
void Student::setIdNum(int num) { idNum = num; } int Student::getIdNum() return idNum; double Student::getRegFee() return RegistrationFee; #include<iostream> class Student { private: int idNum; static double RegistrationFee; public: void setIdNum(int); int getIdNum(); double getRegFee(); };
21
Example…Cont. int main() { Student Std1, Std2; Std1.setIdNum(1234); Std2.setIdNum(2345); cout << “Std1: " << Std1.getIdNum() << " Registration fee: " << Std1.getRegFee() <<endl; cout << “Std2: " << Std2.getIdNum() << " Registration fee: " << Std2.getRegFee() <<endl; return 0; } Result: Std1: 1234 Registration Fee: 50.5 Std2: 2345 Registration Fee: 50.5
22
Static Class Member ..Cont.
Static member can be accessed without using an object (if it is public) Ex: cout << "The Registration fee for all students is " << Student::RegistrationFee << endl; Or Student::RegistrationFee = ; OR Std1.RegistrationFee= 45 Because Static fields frequently are constant use const static double RegistrationFee; Constant static fields hold one unchangeable value for every object. A static function is one you can use with or without a declared object A non-static function can use a static field, but a static function cannot use a non-static field.
23
This Pointer Use This pointer in a function:
Within a function like getIdNum(), the address of the calling object is stored in a special pointer called the this pointer. The this pointer holds the memory address of the current object that is using the function The this pointer is automatically supplied every time you call a non-static member function of a class. The this pointer is a constant pointer. You cannot modify it. When you make the function call Std1.getIdNum() the actual function call made by the compiler is getIdNum(&Std1) The actual argument list used by the compiler for the getIdNum()function is getIdNum(Student *this). Use This pointer in a function: void Student::setIdNum(int id) { (*this).idNum = id; } int Student ::getIdNum() return (*this).idNum;
24
Pointers to objects class Point { // Declaration of Point Class
public: int x,y; void move(int, int); }; int main() { Point *p; //declare pointer to an object P= new point; (*p).x =20; P -> y =50; P -> move(100,50); //or (*p).mive(100,50) return 0; }
25
Implement Time class class Time { int hour; int minute; int second ; public: (Inline)int GetHour() {return hour;} ; int GetMinute() {return minute;} ; int GetSecond () { return second;} ; Void time(int h=0,int m=0,int s=0); void SetTime(int h,int m,int s); void SetHour(int h); void SetMinute(int m); void SetSecond(int s); void PrintTime(); } Hour,minut,second private attribute we update or gets iys value throught the function
26
Time class…cont. Void Time::SetTime(int h,int m,int s) {hour=h;minute=m;second=s;}//or Time::hour=h;… void Time ::SetHour(int h) {hour= h;} void Time ::SetMinute(int m) {minute= m;} void Time ::SetSecond (int s) {second= s;} void Time ::PrintTime (){ Cout<<hour<<“:”<<minut<<“:”<<second<<endl; } Void Time::time (int h=0,int m=0,int s=0) {hour=h;minute=m;second=s;} :: Scope resolution operator
27
Time class…cont. Main(){ Time t(9,12,46); t. GetHour(); t. GetMinute(); t. GetSecond (); t. SetTime(10,00,00); t. SetHour(11); t. SetMinute(15); t. SetSecond(30); t. PrintTime(); } 9 12 46 11:15:30
28
Student class #include<iostream.h> class Student { private: int idNum; Char * lastName; double gradePointAverage; public: void displayStudentData(); void setIdNum(int); void setLastName(string); void setGradePointAverage(double); };
29
Student class …cont. int main() { Student aStudent; Student anotherStudent; aStudent.setLastName("Smith"); aStudent.setIdNum(3456); aStudent.setGradePointAverage(3.5); aStudent.displayStudentData(); anotherStudent.setLastName(“Ali"); anotherStudent.setIdNum(34567); anotherStudent.setGradePointAverage(4.5); anotherStudent.displayStudentData(); return 0; } Result: Student #3456’s last name is Smith The grade point avarage for this student is 3.5 Student #9999’s last name is Ali The grade point avarage for this student is 0 two colons (::)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.