Menoufia University
Faculty of Electronic Engineering
CSE 121: Computer Programming
Dr. Ahmed Hamdy Ahmed Arafa
Computer Science & Engineering Dept
Email:
[email protected] Mobile: +201020069729
Office: CSE Building 4th floor, Room 413
Course Objectives
✓ Overview of basic concepts of C++
✓ Functions in C++
✓ Macros in C++
✓ Arrays and Strings in C++
✓ Overview of basic concepts of object oriented programming in C++
✓ Classes and Objects in C++
✓ Inheritance in C++
✓ Friendship Classes and Methods
✓ Function Overriding in C++
Inheritance Example.
#include<iostream> class Triangle : public Shape
using namespace std; {
class Shape public:
{ Triangle (float x, float y): Shape(x,y)
public: {
float a; }
float b; float triangle_area()
public: {
Shape(float n,float m) return (0.5*a*b);
{ }
a=n; };
b=m; int main()
} {
};
class Rectangle : public Shape int l,w,b,h;
{ cout << "Enter the length and width of a rectangle:" <<endl;
public: cin>>l>>w;
Rectangle (float x, float y): Shape(x,y) Rectangle r(l,w);
{ cout << "Area of the rectangle is:" <<r.rectangle_area()<<endl;
} cout << "Enter the base and height of the triangle:" <<endl;
int rectangle_area() cin>>b>>h;
{ Triangle t (b,h);
return (a*b); cout <<"Area of the triangle is:" <<t.triangle_area()<<endl;
} return 0;
}; }
Accessing Class Private Members.
▪ Non-member functions cannot access the private members of a particular class.
#include <iostream> #include <iostream> #include <iostream> #include <iostream>
using namespace std; using namespace std; using namespace std; using namespace std;
class A class A class A class A
{ { { {
private: protected: private: protected:
int x=4; int x=4; int x=4; int x=4;
}; }; }; };
int main () int main () class B class B
{ { { {
A b; A b; public: public:
cout<<b.x; cout<<b.x; void display (A &a) void display (A &a)
return 0; return 0; { {
} } cout<<a.x; cout<<a.x;
} }
}; };
int main () int main ()
{ {
A a; A a;
B b; B b;
b. display (a); b. display (a);
return 0; return 0;
} }
Friendship Methods / Functions.
What and when to use the friend function
▪ Non-member functions cannot access the private members of a particular class.
▪ Once declared as a friend function, the function can access private, protected members of a class without inheriting the class.
▪ Friend function is considered as a granted permission to access private and protected members of the class in C++.
▪ Used when need to operate between two different classes at the same time.
▪ This is only the case when we do not want to use the objects of that class to access these private or protected members.
▪ It is generally used to modify or change private and protected data members of the class.
Characteristics of the friend functions
▪ It is not a member function nor in the scope of the class to which it has been declared a friend.
▪ It is not restricted to only one class.
▪ It cannot access the member names directly and has to use dot operator (.) and use an object name with the member name.
▪ Declared only (not defined nor called) using the friend keyword inside the body of the class that needs to be accessed.
▪ A friend function can be declared in any section of the class i.e. public or private or protected.
▪ Friend functions are not inherited.
▪ Friend functions have access to private members of a class from outside the class which violates the law of data hiding.
▪ Occupy the maximum size of the memory and can’t do any run-time polymorphism concepts.
Implementation of friend functions
✓ Method / Function of another class : Friend Function exists in another class other than the class to be accessed
✓ Global function: The Friend function is a global function and not a member of any class.
✓ Friend Class: Friend class is created that contains one or more functions all are friend functions
Implementation of the friend functions: Method / Function of another class
✓ Friend function is declared as a member function of another class and called using an object of this class.
✓ The order in which we define the friend function of another class is important and should be considered.
✓ We must define both of the classes before the friend function definition.
✓ The class to be accessed must has a forward definition before the class containing the friend function.
✓ Out of class member function definition is used for the friend function.
#include <iostream>
using namespace std; int main ()
class A; // forward definition of class A {
class B A a;
{ B b;
public: b.get_x(a);
void get_x(A a); return 0;
}; }
class A
{
private:
int x=4;
public:
friend void B::get_x(A a);
};
void B::get_x(A a)
{
cout<<a.x;
}
Implementation of the friend functions: Global Function
✓ Friend Function is declared inside the class that needs to be accessed.
✓ Friend Function is not part on any class (global function)
✓ Friend function is called as normal function without using an object nor the dot operator.
#include <iostream>
using namespace std;
class A
{
private:
int x=4;
public:
friend void get_x(A a);
};
void get_x(A a)
{
cout<<a.x;
}
int main ()
{
A a;
get_x(a);
return 0;
}
Implementation of the friend functions: Friend Class
✓ Friend class is used when we need to access private and protected members of the class in which it has been declared as a friend.
✓ All functions in the friend class are friend functions of the other class required to be accessed.
✓ If class A is a friend of B, then B doesn’t become a friend of A automatically (Friendship is not mutual).
#include <iostream>
class class_name using namespace std;
{
friend class friend_class; class A
}; {
private:
class friend_class int x=4;
{ friend class B; //friend class
};
}; class B
{
public:
void get_x (A a)
{
cout<<a.x;
}
};
int main ()
{
A a;
B b;
b. get_x (a);
return 0;
}
Friend Function Example
#include <iostream>
using namespace std;
class Box
{
private:
int length=0;
public:
friend int printLength(Box);
};
int printLength(Box b)
{
b.length += 10;
return b.length;
}
int main()
{
Box b;
cout<<"Length of box: "<< printLength(b)<<endl;
return 0;
}
Function Overriding in C++.
✓ Redefinition of base class function in its derived class with the same signature i.e. return type and parameters.
✓ A function cannot be overridden multiple times as it is resolved at Run time.
✓ Overridden functions must have the same function signatures ( return type, name, parameters).
✓ Function Overriding occurs when the derived class and the base class functions are expected to perform differently.
✓ Can’t be executed without inheritance.
✓ Overridden functions are of different scopes.
#include <iostream>
using namespace std;
class employee
class manager : public employee
{
{
public:
public:
float salary;
manager(float sal): employee(sal)
public:
{
employee(float salary)
}
{
float get_bonus( float salary)
this->salary=salary;
{
}
return salary*0.2;
float get_bonus( float salary)
}
{
};
return salary*0.1;
}
};
Function Overriding in C++ (cont.).
int main()
{
float sal;
cout<<"Enter The salary"<<endl;
cin>>sal;
employee e(sal);
cout<<"Employee Bounus: "<< e.get_bonus(sal)<<endl;
manager m(sal);
cout<<"Manager Bounus: "<< m.get_bonus(sal)<<endl;
return 0;
}
Any Questions?????