160 likes | 174 Views
Explore object interfaces, access specifiers, friend functions, arrays of objects, pointers to objects, object assignments, and passing objects to functions in C++. Dive into encapsulation and object-oriented programming concepts.
E N D
Recap: Interfaces • A class is a logical abstraction, but an object has physical existence. • access-specifier can be: • public: Allow functions or data to be accessible to other parts of the program. • private: May be accessed only by other members of the class. • protected: Used when inheritance is involved; not discussed yet, will do later today
friends #include <iostream> using namespace std; class myclass { int a, b; public: friend int sum(myclass x); //declared in the public interface void set_ab(int i, int j); }; void myclass::set_ab (int i, int j) { a = i; b = j; } // Note: sum() is not a member function of any class. int sum (myclass x) { //Because sum() is a friend of myclass, it // can directly access a and b. return x.a + x.b; } main() { myclass n; n.set_ab (3, 4); cout << sum(n); return 0; } //see friend1.cpp
Friends • A friend function has access to all private and protected (and public of course) members of the class for which it is a friend. • See friend1.cpp example • See also friend1b.cpp
//use of friend function to access private data //members of class class Myclass{ private: int a,b; public: Myclass(int i, int j){a=i; b=j;} friend int sum(myclass x); }; //note sum is not a member function of any class int sum (Myclass x) {return x.a+x.b;} int main(){ Myclass n(3,4); cout << sum(n) << "\n"; return 0; }
Friends • The friend function negates information hiding • This is the whole idea of OO programming i.e. encapsulation • We now have a way of destroying this property • For this reason many programmers are not keen on the use of friend functions • We will return to friends again later in this presentation.
Arrays of Objects • We can create arrays of objects just as we can create an array of integers or doubles • See ArrayofObjects1.cpp • See ArrayofObjects2.cpp //this initialises //an array of // objects • See ArrayofObjects3.cpp //2D array of //objects //ignore last program
Pointers to Objects • Recall we access a structure directly or indirectly using a pointer to that structure. • In like fashion we can access an object either directly or indirectly using pointers again • To access an element of an object when using the actual object we use the dot notation • To access a specific element of an object using the pointer we use -> (arrow link) • See objectpointer1.cpp
Incrementing pointers • As we know when a pointer is incremented(or decremented) it is increased (or decreased) in such as way that it will always point to next element of its base type • See objectPointer2.cpp
More on friends • So it is possible to allow a non-memberfunction of any class to access the private members of a class by declaring it as a friend of the class. • To make a function a friend of a class you include its prototype in the public section of a class declaration and precede it with the friend keyword
Demo • See friend2.cpp and explain
#include <iostream> using namespace std; class Myclass { int a, b; public: friend int sum(myclass x); void set_ab(int i, int j); }; void Myclass::set_ab (int i, int j) { a = i; b = j; } // Note: sum() is not a member function of any class. int sum (Myclass x) {//Because sum() is a friend of myclass, it can directly access a and b. return x.a + x.b; } main() { Myclass n; n.set_ab (3, 4); cout << sum(n); return 0; }
Assigning objects • If both objects are of the same type (i.e. both objects are of the same class) then one object may be assigned to another. • The following program demonstrates this • See objectassignment1.cpp
#include <iostream> using namespace std; class Myclass{ private: int a,b; public: void setab(int i,int j){a=i;b=j;} void showab(); }; void Myclass::showab(){ cout << "a is " << a << endl; cout << "b is " << b << endl; }
int main(){ Myclass ob1,ob2; ob1.setab(10,20); ob2.setab(0,0); cout << "ob1 before assignment" << endl; ob1.showab(); cout << endl; cout << "ob2 before assignment" << endl; ob2.showab(); cout << endl; ob2=ob1; cout << "ob1 after assignment" << endl; ob1.showab();cout << endl; cout << "ob2 after assignment" << endl; ob2.showab();cout << endl; return 0; }
Passing objects to functions • An object can be passed to a function In the same way as any other data type. • Objects are passed to functions by using the normal C++ call by-value parameter-passing convention. • This means that a copy of the objects and not the object itself is passed to the function. • Thus any changes made to the object inside the function do NOT affect the object used as argument to the function
Demonstration • See passingobjects1.cpp and explain • See passingobjectsbyRef.cpp and explain • The comments indicate that the modification to x within f() has no affect on the object 0 inside main()