本视频主要从Java语言基础、JavaSE核心、WEB全栈及数据库、Servlet/Jsp核心、 Java框架五个阶段进行讲解。在本教程中,会让大家从零基础快速掌握Java知识。想要配套学习资料的小伙伴可以联系我哦!
c++继承经典例子
#include
{
private:
int b_number;
public:
Base( ){}
Base(int i) : b_number (i) { }
int get_number( ) {return b_number;}
void print( ) {cout << b_number << endl;}
};class Derived : public Base
{
private:
int d_number;public:
// constructor, initializer used to initialize the base part of a Derived object.
Derived( int i, int j ) : Base(i), d_number(j) { };
// a new member function that overrides the print( ) function in Base
void print( )
{
cout << get_number( ) << " ";
// access number through get_number( )
cout << d_number << endl;
}
};int main( )
{
Base a(2);
Derived b(3, 4); cout << "a is ";
a.print( ); // print( ) in Base
cout << "b is ";
b.print( ); // print( ) in Derived
cout << "base part of b is ";
b.Base::print( ); // print( ) in Base return 0;
}
没有虚析构函数,继承类没有析构
//Example: non- virtual destructors for dynamically allocated objects. #include
#include
{ public:
virtual void what_Am_I( ) {cout << "I am a Thing.\n";}
~Thing(){cout<<"Thing destructor"<
{
public:
virtual void what_Am_I( ) {cout << "I am an Animal.\n";}
~Animal(){cout<<"Animal destructor"<
{
Thing *t =new Thing;
Animal*x = new Animal;
Thing* array[2]; array[0] = t; // base pointer
array[1] = x; for (int i=0; i<2; i++) array->what_Am_I( ) ; delete array[0];
delete array[1];
return ;
}
纯虚函数,多态 #include
#include
{
private:
double x;
double y;
public:
Point(double i, double j) : x(i), y(j) { }
void print( ) const
{ cout << "(" << x << ", " << y << ")"; }
};class Figure
{
private:
Point center;
public:
Figure (double i = 0, double j = 0) : center(i, j) { }
Point& location( )
{
return center;
} // return an lvalue
void move(Point p)
{
center = p;
draw( );
} virtual void draw( ) = 0; // draw the figure
virtual void rotate(double) = 0;
// rotate the figure by an angle
};class Circle : public Figure
{
private:
double radius;
public:
Circle(double i = 0, double j = 0, double r = 0) : Figure(i, j), radius(r) { }
void draw( )
{
cout << "A circle with center ";
location( ).print( );
cout << " and radius " << radius << endl;
}
void rotate(double)
{
cout << "no effect.\n";
} // must be defined
};class Square : public Figure
{
private:
double side; // length of the side
double angle; // the angle between a side and the x-axis
public:
Square(double i = 0, double j = 0, double d = 0, double a = 0) : Figure(i, j), side(d), angle(a) { }
void draw( )
{
cout << "A square with center ";
location( ).print( );
cout << " side length " << side << ".\n"
<< "The angle between one side and the X-axis is " << angle << endl;
}
void rotate(double a)
{
angle += a;
cout << "The angle between one side and the X-axis is " << angle << endl;
}
void vertices( )
{
cout << "The vertices of the square are:\n";
// calculate coordinates of the vertices of the square
}
};int main( )
{
Circle c(1, 2, 3);
Square s(4, 5, 6);
Figure *f = &c, &g = s; f -> draw( );
f -> move(Point(2, 2)); g.draw( );
g.rotate(1);
s.vertices( );
// Cannot use g here since vertices( ) is not a member of Figure. return 0;
}
////////////////////////////////////////////////////////////////////
#include
#include
{
public:
virtual void what_Am_I( ) {cout << "I am a Thing.\n";}~Thing(){cout<<"Thing destructor"<
{
public:
virtual void what_Am_I( ) {cout << "I am an Animal.\n";}~Animal(){cout<<"Animal destructor"<
{
Thing t ;
Animal x ;
Thing* array[2]; array[0] = &t; // base pointer
array[1] = &x;
for (int i=0; i<2; i++) array->what_Am_I( ) ; return ;
}私有继承
// Access levels#include
{
private:
int priv;
protected:
int prot;
int get_priv( ) {return priv;}
public:
int publ;
Base( );
Base(int a, int b, int c) : priv(a), prot(b), publ(c) { }
int get_prot( ) {return prot;}
int get_publ( ) {return publ;}
};class Derived1 : private Base // private inheritance
{
public:
Derived1 (int a, int b, int c) : Base(a, b, c) { }
int get1_priv( ) {return get_priv( );}
// priv not accessible directly
int get1_prot( ) {return prot;}
int get1_publ( ) {return publ;}
};class Leaf1 : public Derived1
{
public:
Leaf1(int a, int b, int c) : Derived1(a, b, c) { }
void print( )
{
cout << "Leaf1 members: " << get1_priv( ) << " "
// << get_priv( ) // not accessible
<< get1_prot( ) << " "
// << get_prot( ) // not accessible
// << publ // not accessible
<< get1_publ( ) << endl;
} // data members not accessible. get_ functions in Base not accessible
};class Derived2 : protected Base // protected inheritance
{
public:
Derived2 (int a, int b, int c) : Base(a, b, c) { }
};class Leaf2 : public Derived2
{
public:
Leaf2(int a, int b, int c) : Derived2(a, b, c) { }
void print( )
{
cout << "Leaf2 members: " << get_priv( ) << " "
// << priv // not accessible
<< prot << " "
<< publ << endl;
} // public and protected data members accessible. get_ functions in Base accessible.
};class Derived3 : public Base // public inheritance
{
public:
Derived3 (int a, int b, int c) : Base(a, b, c) { }
};class Leaf3 : public Derived3
{public:
Leaf3(int a, int b, int c) : Derived3(a, b, c) { }
void print( )
{
cout << "Leaf3 members: " << get_priv( ) << " "
<< prot << " "
<< publ << endl;
} // public and protected data members accessible. get_ functions in Base accessible
};int main( )
{
Derived1 d1(1, 2, 3);
Derived2 d2(4, 5, 6);
Derived3 d3(7, 8, 9);// cout << d1.publ; // not accessible
// cout << d1.get_priv( ); // not accessible
// cout << d2.publ; // not accessible
// cout << d2.get_priv( ); // not accessible
cout << d3.publ; // OK
cout << d3.get_prot( ); // OK Leaf1 lf1(1, 2, 3);
Leaf2 lf2(4, 5, 6);
Leaf3 lf3(7, 8, 9);// cout << lf1.publ << endl; // not accessible
// cout << lf2.publ << endl; // not accessible
cout << lf3.publ << endl; // OK return 0;
}