Presentation is loading. Please wait.

Presentation is loading. Please wait.

IT533 Adapted from Textbook (SAVITCH) and Lecture Notes (PUSATLI), 2014.

Similar presentations


Presentation on theme: "IT533 Adapted from Textbook (SAVITCH) and Lecture Notes (PUSATLI), 2014."— Presentation transcript:

1 IT533 Adapted from Textbook (SAVITCH) and Lecture Notes (PUSATLI), 2014

2 Friend Function An ordinary function
However, it is not a member function It can access private members of objects It is a public function no matter where it is declared. Most of them are overload operands but any function can be a friend function.

3 Syntax Use keyword “friend” in front. “ClassName::” is not used.
A function is accepted as a “friend” to a class; hence, “friend” keyword is put only in the function declaration, not in the function definition.

4 Example void print (Integer &in) {
cout<<"number = "<<in.number; } class Integer{ private: int number; public: Integer (int n=0):number(n){} friend void print(Integer&); }; main(){ Integer in(1); print(in); system("pause");

5 Example #include<iostream> using namespace std; class Integer{
int number; public: Integer (int n=0):number(n){} friend void print(Integer&); }; void print (Integer &in) { cout<<"number = "<<in.number; } main(){ Integer in(1); print(in); // NOT in.print() as print is not a member function system("pause");

6 Example main(){ #include <iostream>
Complex c1(1.1, 2.2), c2(3.3, 4.4); Complex c3=add(c1,c2); c3.print(); system("pause"); } #include <iostream> using namespace std; class Complex{ float real; float imag; public: Complex(float=0.0, float=0.0); void print(){cout <<real<<"+"<<imag<<"i"<<endl;} friend Complex add(Complex&, Complex&); }; Complex::Complex(float r, float i): real(r), imag(i){} Complex add(Complex &c1, Complex &c2){ return Complex(c1.real+c2.real, c1.imag+c2.imag); }

7 Exercise Write a program that can multiply a matrix of 4x4 by a vector of 4 elements and gives the product. e.g. b0= a00v0+ a01v1+ a02v2+ a03v3 a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23 a30 a31 a32 a33 v0 v1 v2 v3 b0 b1 b2 b3 = X

8 The this Pointer The this pointer is a predefined pointer that points to the calling object. this is not the name of the calling object; it is the name of a pointer that points to the object. It may look like useless but sometimes it may be handy.

9 The this Pointer The class objects maintain their own data members; however, they share a single copy of each member function stored in the memory. Following example is a re-coding by using this pointer while calling a member function.

10 Example Both codes will generate same output.
#include <iostream> using namespace std; class MyNumber{ public: int t; void print(){ cout<< t <<endl; } }; main(){ MyNumber n1; n1.t=5; n1.print(); system("pause"); #include <iostream> using namespace std; class MyNumber{ public: int t; void print(){ cout<< this->t <<endl; } }; main(){ MyNumber n1; n1.t=5; n1.print(); system("pause"); Both codes will generate same output.

11 Example #include <iostream> using namespace std; class MyNumber{
private: int number; public: MyNumber (int n=0){number=n;} MyNumber increment(int n){ number+=n; return MyNumber(number); } void print(){ cout<<"number = "<<number<<endl; }; MyNumber& increment(int n){ number+=n; return *this; } main(){ MyNumber n1(1); n1.increment(2).increment(3).print(); system("pause"); }

12 Dynamic Memory Allocation
Dynamic Variables are: variables created by the new operator created and destroyed while the program is running

13 new By using new operator, a pointer can be used to refer to a variable even if the variable has no identifiers to name it. new operator serves to acquire a memory space at execution time. int *ptr; ptr = new int; OR int *ptr = new int;

14 Example class Rational{ int x; public: void display() {...} }; main(){
Rational *rptr; // OR Rational *rptr=new Rational; rptr=new Rational; rptr->display(); }

15 Uses Dynamic memory allocation is used to:
allocate and initialize a dynamic object allocate space for multiple dynamic objects int *ip = new int(12); Rational *rp=new Rational(3,4); Rational *rlist =new Rational[5]; rlist[1].display();

16 Uses Dynamic memory allocation is used to: delete a dynamic object
delete multiple dynamic objects char *ptr1 = new char(‘A’); char *ptr2=ptr1; delete ptr1; cout<<*ptr2; // ERROR! employee *ptr=new employee[10]; delete [] ptr;

17 Example What is the output? #include <iostream>
using namespace std; class Ex{ int *n; public: Ex(int=0); ~Ex(); void print(){ cout<<*n<<endl; } }; Ex::Ex(int i){ n=new int(i); Ex::~Ex(){ delete n; main(){ Ex obj1(5), obj2; obj1.print(); obj2.print(); system("pause"); } What is the output?

18 Static Class Data Members
When you declare a data member in a class, all the objects of that class has a copy of that data member as its own. However, you can declare a data member to be shared by all instances i.e. across all instances of the class.

19 Example #include <iostream> using namespace std;
class KeepCount{ static int counter; // static declaration // ‘counter’ is global to all instances public: KeepCount() {counter=counter+1;} void print(){cout <<"counter = " <<counter<<endl;} }; int KeepCount::counter=0; // static definition main(){ KeepCount kc1, kc2, kc3; kc1.print(); kc2.print(); system("pause"); } Example // WHAT IF … main(){ KeepCount kc1, kc2, kc3; kc1.print(); kc2.print(); KeepCount *ptr=new KeepCount; system("pause"); }

20 Operator Overloading This is to assign new functions to existing operators For example: addition (+) The operator “+” is functional for integer type; but it is functional for double type as well. Because it is already overloaded

21 Properties Operator overloading functions can be member or friend functions All operators can be overloaded except * :: ?: () and []  operators can only be overloaded as member functions Only existing operators can be overloaded Operator overloading does NOT give additional functionality to the code

22 Example You would like to use addition operator (+) for complex numbers as well. #include <iostream> using namespace std; class Complex{ float real, imag; public: Complex (float r, float i){real=r; imag=i;} friend Complex operator+(Complex&, Complex&); void print(){cout <<real <<"+"<<imag<<"i"<<endl;} }; /* Operator overloading as a friend function */ Complex operator+(Complex &a, Complex &b){ return Complex(a.real+b.real, a.imag+b.imag); } main(){ Complex x(2,3), y(5,8); Complex z=x+y; z.print(); system("pause"); }

23 Example Overload += operator for complex numbers
#include<iostream> using namespace std; class Complex{ float real, imag; public: Complex(float r, float i){real=r; imag=i;} void print(){cout <<real <<"+"<<imag<<"i"<<endl;} void operator +=(Complex&); }; /* Operator overloading as a member function */ void Complex::operator+=(Complex &a){ real=real+a.real; imag=imag+a.imag; } main(){ Complex x(2,3), y(5,4); x+=y; x.print(); int a=7; a+=2; cout<<a<<endl; a+=y; //ERROR! system("pause"); }

24 Overloading Operators with Objects
#include<iostream> using namespace std; class A{ int number; public: int n1; A(){number=0; cout<<"default constructor"<<endl; } A(A &a){ number=a.number; cout<<"copy constructor"<<endl; ~A(){ cout<<"destructor"<<endl; A& operator= (A &a) { cout<<“assignment“; return *this; } }; main(){ A a1; cout<<1<<endl; A a2(a1); cout<<2<<endl; A a3=a2; cout<<3<<endl; a2=a1; } default constructor 1 copy constructor 2 3 assignment destructor

25 Briefly on Streams A stream is a flow of characters (including numbers). If this flow is into the program then it is an input stream. If the flow is out of the program then is an output stream. cin is an input stream connected to the keyboard. cout is an output stream connected to the screen.

26 Briefly on Streams ostream is a class, which provides output method (function). To call this method, you need an object of that class. cout is an object of the class ostream Similarly, cin is an instantiation of the class istream, which provides input method. << and >> are the operators to call those methods i.e. functions.

27 Overloading Input/Output
You can overload << and >> operators to handle I/O for your own personal data type, such as objects of your class. For example, you may like to print data members of an object to the screen by using cout<<. To do this, you have to overload the operator <<.

28 Example #include <iostream> #include <String>
using namespace std; class Name{ friend ostream& operator << (ostream&, Name&); friend istream& operator >> (istream&, Name&); private: string fName, lName; }; ostream& operator <<(ostream &os, Name &nm){ os<<"first name: " <<nm.fName<<endl; os<<"last name: " <<nm.lName<<endl; return os; } istream& operator >>(istream &inp, Name &nm){ cout<< "Enter first and last name: "; inp>>nm.fName>> nm.lName; return inp; main(){ Name myName; cin >>myName; cout <<myName; system("pause"); } Enter first and last name: Ali Kara first name: Ali last name: Kara Press any key to continue . . .

29 Example #include<iostream> using namespace std; class Complex{
double real, imag; public: Complex(double r=0, double i=0):real(r), imag(i){} friend ostream &operator<<(ostream&, Complex&); }; ostream &operator<< (ostream &stream, Complex &c){ stream<<c.real<<"+"<<c.imag<<"i"<<endl; return stream; } main(){ Complex c1(3,4); cout <<c1; system("pause"); 3+4i Press any key to continue . . .


Download ppt "IT533 Adapted from Textbook (SAVITCH) and Lecture Notes (PUSATLI), 2014."

Similar presentations


Ads by Google