Department of Computer Science & Engineering Air University: Object Oriented Programming Week # 5
The document discusses object oriented programming concepts like passing arguments by value vs reference and the const qualifier.
It provides examples of passing arguments by value and reference in C++ functions. It also explains how the const qualifier can be used to prevent modification of variables and objects, including const objects that can only call const member functions. The document shows examples of using const with function arguments and member functions.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
29 views
Department of Computer Science & Engineering Air University: Object Oriented Programming Week # 5
The document discusses object oriented programming concepts like passing arguments by value vs reference and the const qualifier.
It provides examples of passing arguments by value and reference in C++ functions. It also explains how the const qualifier can be used to prevent modification of variables and objects, including const objects that can only call const member functions. The document shows examples of using const with function arguments and member functions.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20
Department of Computer Science & Engineering
Air University
Object Oriented Programming
Week # 5
By: Saqib Rasheed
#include <iostream> using namespace std; int addition(int a, int b); Passing by value int main () { int x=5,y=3,z; z = addition (x,y); cout << "The result is " << z; } int addition (int a, int b) { int r; r=a+b; return r; } Saqib Rasheed 2 int addition(int &a, int &b); int main () { Passing by reference int x=5,y=3,z;
z = addition (x,y);
cout << "The result is " << z<<endl;
} int addition (int &x, int &y) { int r; r=x+y; return r; } Saqib Rasheed 3 int addition(int &a, int &b); int main () { int x=5,y=3,z;
cout<<"From Main Before x = "<<x<<"y = "<<y<<endl;
z = addition (x,y);
cout<<"From Main After x = "<<x<<"y = "<<y<<endl;
cout << "The result is " << z<<endl; } int addition (int &x, int &y) { cout<<"From Function x = "<<x<<"y = "<<y<<endl; int r; r=x+y; x = 10; y = 20; return r; } The const Qualifier const float PI = 3.1415;
The keyword const (for constant) precedes the data type of a
variable. It specifies that the value of a variable will not change throughout the program. Any attempt to alter the value of a variable defined with this qualifier will elicit an error message from the compiler. The qualifier const ensures that your program does not inadvertently alter a variable that you intended to be a constant, such as the value of PI in CIRCAREA. It also reminds anyone reading the listing that the variable is not intended to change. The const modifier can apply to other entities besides simple variables. We’ll learn more about this as we go along const Function Arguments We’ve seen that passing an argument by reference can be used to allow a function to modify a variable in the calling program. However, there are other reasons to pass by reference. One is efficiency. Some variables used for function arguments can be very large A large structure would be an example. If an argument is large, passing by reference is more efficient because, behind the scenes, only an address is really passed, not the entire variable. Suppose you want to pass an argument by reference for efficiency, but not only do you want the function not to modify it, you want a guarantee that the function cannot modify it. Example const and Classes We’ve seen several examples of const used on normal variables to prevent them from being modified we saw that const can be used with function arguments to keep a function from modifying a variable passed to it by reference. Now that we know about classes… we can introduce some other uses of const on member functions, on member function arguments, and on objects. These concepts work together to provide some surprising benefits. const Objects In several example programs, we’ve seen that we can apply const to variables of basic types Such as int to keep them from being modified.
In a similar way, we can apply const to objects of classes.
When an object is declared as const, you can’t modify it.
It follows that you can use only const member functions with it, because they’re the only ones that guarantee not to modify it. const Objects… A football field (for American-style football) is exactly 300 feet long. If we were to use the length of a football field in a program, it would make sense to make it const Changing it would represent the end of the world for football fans. The program makes football a const variable. Now only const functions, such as showdist(), can be called for this object. Non-const functions, such as getdist(), which gives the object a new value obtained from the user, are illegal. In this way the compiler enforces the const value of football. class Distance { private: Example 1 int feet; float inches; public: //2-arg constructor Distance(int ft, float in) : feet(ft), inches(in) {} void getdist() //user input; non-const func { cout << "\nEnter feet: "; cin >> feet; cout << "Enter inches: "; cin >> inches; } void showdist() const //display distance; const func { cout << feet << "\'-" << inches << '\"'; } }; Example… int main() { const Distance football(300, 0); // football.getdist(); //ERROR: getdist() not const cout << "football = "; football.showdist(); //OK cout << endl; return 0; } const Member Functions A const member function guarantees that it will never modify any of its class’s member data. const Member Functions The non-const function nonFunc() can modify member data alpha, but the constant function conFunc() can’t. If it tries to, a compiler error results. A function is made into a constant function by placing the keyword const after the declarator but before the function body. If there is a separate function declaration, const must be used in both declaration and definition. Member functions that do nothing but acquire data from an object are obvious candidates for being made const, because they don’t need to modify any data Example 2 class Distance //Distance class { private: int feet; float inches; public: //constructor (no args) Distance() : feet(0), inches(0.0) { } //constructor (two args) Distance(int ft, float in) : feet(ft), inches(in) {} Example… void getdist() //get length from user { cout << "\nEnter feet: "; cin >> feet; cout << "Enter inches: "; cin >> inches; } void showdist() const //display distance { cout << feet << "\'-" << inches << '\"'; } Distance add_dist(const Distance&) const ; //add }; Example… Distance Distance::add_dist(const Distance& d2) const { Distance temp; //temporary object //feet = 0; //ERROR: can’t modify this // d2.feet = 0; //ERROR: can’t modify d2 temp.inches = inches + d2.inches; //add the inches if total exceeds 12.0, if(temp.inches >= 12.0) { temp.inches -= 12.0; //minus 12.0 temp.feet = 1; //increase feet//by 1 } temp.feet += feet + d2.feet; //add the feet return temp; } Example… int main() { Distance dist1, dist3; //define two lengths Distance dist2(11, 6.25); //define, initialize dist2 dist1.getdist(); //get dist1 from user dist3 = dist1.add_dist(dist2); //dist3 = dist1 + dist2 //display all lengths cout << "\ndist1 = "; dist1.showdist(); cout << "\ndist2 = "; dist2.showdist(); cout << "\ndist3 = "; dist3.showdist(); cout << endl; return 0; } How to Build Header files distance.h class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0)
};
Save this file as distance.h
Header files is always saved on the name of class
.cpp files #include “distance.h” #include<iostream> Using namesapce std; Int main() { distance d1(10,2.2); }