Inheritance(to reuse the code functionality):
What is that:
Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.(instead of writing completely new data members and member functions of existing class.)
Access:
There are three layers fo access:
Public: We have seen before, anyone can access with dot-notation.
Private: We have seen before, cannot be accessed without assistance from a public member function.(set,get)
Protected: New, available to the base class and the derived class, but cannot be accessed without assistance from a public member function.
Base Class: Also called Parent class or Superclass.(starting point of inheritance)
Derived Class: Also called the Child class or the Subclass.
It can access all the non-private member of its base class.
Data Members: The variables declared inside the class are known as data members(may be private or public)
Member functions: The functions declared inside the class are known as member functions( Member function are methods or functions that are defined inside of objects. Generally used to manipulate data members and other object data.)
Encapsulation: A class, contain associated data and functionality. Within the class, this data and functionality have different scope and access. (Putting like data and functions together is encapsulation)
- The ability to have base classes, derived classes, virtual functions, and private and protected members are the key elements of object-oriented programming
- Almost all Graphical User Interfaces(GUI) and Graphics are controlled via Object-Oriented Programming
The relationship of Inheritance:
***A derived class inherits all base class methods with the following exceptions −***(Chinese notation:never mind, if you are a foreigner. 以下为子类不能继承的功能,其中有析构函数,友元函数,课上没有讲,可忽略。)
- Constructors, destructors and copy constructors of the base class.
- Overloaded operators of the base class.
- The friend functions of the base class.
Sample code:
C++
//Base class
class Father(){
protected:
int a1;
private:
int a2;
public:
Father();
void printFatehr()
};
//define function in the base class
Father:: Father(){
a1 = 1;
a2 = 2;
}
void printFahter(){
cout << a1 << " " << a2;
}
//Derived class
class Child:public Father(){
protected:
int b1;
private:
int b2;
public:
Child();
void printChild();
};
/*** data member of base class
a2 is not in the Child class,
but a2 is in the Child class.***/
JAVA:
(semicolon is not important in java)
public class Subclass extends SuperClass{
}
Python:
"""The class Child(subclass) in the code inherits
attributes from its parent class,
Father(superclass).
"""
class Child(Father):
Polymorphism:
Overloaded Operators:
Operator overloading allows you to redefine the way operator works for user-defined types only(objects, structures). It cannot be used for built-in types(int, float, char etc.)
- Overloaded Operations in C++
Writing operation overloading not as a function member of the class:
you have to use two parameters:
class complex
{
public:
complex(double r = 0.0, double i = 0.0)
{real = r; imag = i}//constructor
private:
double real;
double imag;
};
complex operator+(const complex &a, const complex &b);
//const mean this variable cannot be changed.
int main(){
complex c1(1,2), c2(3,4), c3;
c3 = operator+(c1+c2);
c3 = c1 + c2;//The function same as above
}
Writing operation overloading as a function member of the class:
you can only use one parameter.(coz there is a “this” pointer in the non-static member function you can google that by yourself, but it is not really important for this exam.)
For Arithmetic operators(+,-,*,/,%)
//complex number operation
#include <iostream>
using namespace std;
class complex
{
public:
complex(double r = 0.0, double i = 0.0)
{real = r; imag = i;}//constructor
complex operator +(complex &c2);
complex operator +(complex &c2);
void display()
{
cout << real << "+" << imag << "i" << endl;
}
private:
double real;
double imag;
};
complex complex::operator +(complex &c2)
{
complex c;
c.real = c2.real+real;
c.imag = c2.imag+imag;
return c;
}
complex complex::operator -(complex &c2)
{
complex c;
c.real = real - c2.real;
//real is the invoker's real("this" pointer)
c.imag = imag - c2.imag;
return c;
}
int main(){
complex c1(1,2), c2(3,4), c3;
c3 = c2+c1; c3.display();// output: 4+6i
c3 = c2-c1; c3.display();// output: 2+2i
//c3 = c2+c1 means c2.operator+(c1)
return 0;
}
For increment or decrement(++ --):
- for +first: ++i
#include <iostream>
using namespace std;
class complex
{
public:
complex(double r = 0.0, double i = 0.0)
{real = r; imag = i;}//constructor
complex operator ++();
void display()
{
cout << real << "+" << imag << "i" << endl;
}
private:
double real;
double imag;
};
complex complex::operator ++()
{
complex a;
real ++;
imag ++;
a.real = real;
a.imag = imag;
return a;
}
int main()
{
complex c1(1,2), c2;
c2 = ++c1;
//c2 = ++c1 means c2.operator ++()
c1.display();// output: 2+3i
c2.display();// output: 2+3i
return 0;
}
- for i++
complex complex::operator ++()
{
complex a;
a.real = real;
a.imag = imag;
real ++;
imag ++;
return a;
}
int main()
{
complex c1(1,2), c2;
c2 = ++c1;
//c2 = ++c1 means c2.operator ++()
c1.display();// output: 2+3i
c2.display();// output: 1+2i
return 0;
}
- Overloaded Operations in python
>>>class Computation():
` def __init__(self, value):
self.value = value
def __add__(self, other):
return self.value + other
def __sub__(self, other):
return self.value - other
"""Overloading these two methods __add__
& __sub__, you can do "+","-" on the objects"""
- Overloaded Operations not in java
Notation!:
- Operator overloading cannot change the precedence and associatively of operators. However, if you want to change the order of evaluation, parenthesis should be used.
- There are 5 operators that cannot be overloaded in C++. They are :: (scope resolution),. (member selection), .* (member selection through pointer to function) and ?: (ternary operator) and sizeof