1: Abstract class VS data abstraction
#include <iostream>
using namespace std;
/*
a class is declared as abstract by declaring at least one of its function as
pure virtual function. A pure virtual function is specified by placing "=0" in
its declaration.
Abstract classes can not be used to instantiate objects and servers only as interface.
*/
class Shape{
public:
virtual int getArea() = 0;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle : public Shape {
public:
int getArea() {
return (width * height);
}
};
class Triangle : public Shape {
public:
int getArea() {
return (width * height) / 2;
}
};
Data Abstraction refers to, providing only essential information to outside world and hiding their background details (public, and private, protected). The child class should re-implement all the virtual functions.
In the above example, we can declare two Polygon pointers and assign them the address of Triangle and Rectangle class. For example
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << '\n';
cout << trgl.area() << '\n';
return 0;
}
Dereferencing ppoly1 and ppoly2 is valid and allows us to access the members of their pointed objects. However, because ppoly1 and ppoly2 is pointer to Polygon, only members inherited from Polygon can be accessed, and not those of the derived classes Rectangle and Triangle.
If in the above example, the base class remove the virtual keyword, all the two calls to area function will point to the base class and thus result will be 0.
Notice that: Pure virtual function is different from Virtual function. Pure Virtual function doesn't have any implemention of the function, virtual function might have.
1-> How to guarantee one class can not be inherited
Two ways, first declare the class to be final. Second, make the constructor to be private.
Reasons of memory leak in C++: 1: new something, forget to delete, alloc something, forget the dealloc. 2: Smart pointer, one source of leaks with reference-counting smart pointers are pointers with circular dependancies. For example, A have a smart pointer to B, and B have a smart pointer to A, neither A or B will be destroyed. We will have to find and then break the dependencies.
The use of Const:
1: simple in concept: Variable declared with 'const' added become constants and cannot be altered by the program. For example, const int Constant_1 = 96; will create an integer const.
2: It can also works with pointers. const int* constant_2; which declares that constant_2 is a variable pointer to a constant integer.
int const* const_2 declares constant_2 is a variable pointer to a constant integer.
int* const const_3 declares that const_3 is constant pointer to a variable integer.
int const* const const_4, declares that const_4 is constant pointer to a constant integer.
3: use of "const" in function return values.
const char* function() {return "some text";} the compiler would know that the value was unalterable.
4: use of const in parameter passing
void subroutine(big_structure_type const ¶meter) will cause the variable to be passed without copying but stop it from then being altered.
5: In Object-Oriented Programming
for example:
class Class1 {
public:
void method();
private:
int member;
}
void Class1::method() {member++;} In this case, method() function can revise the private member variable.
However, if we declare the method as : void method() const; this function will not being able to revise private member variable.
Static Keyword:
class members static using static keyword: no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class.
Static function: by declaring a function member as static, we can make it independent of any particular object of the class. Static member function can only access static data member, other static member function and any other functions from outside the class.