Commit 2a291a73 authored by shengnan hu's avatar shengnan hu
Browse files

main

parents
#include <iostream>
using namespace std;
class Foo
{
public:
int a;
Foo ( void );
explicit Foo ( int );
private:
int b;
};
Foo::Foo ( void ) //遵循1
{
a = 0;
b = 0;
}
Foo::Foo ( int varb ) //遵循2
{
a = 0;
b = varb;
}
int main ( void )
{
Foo thef1, thef2 ( 1 );
return ( 0 );
}
#include <iostream>
using namespace std;
class Docement
{
public:
int docid;
Docement ( void ) : docid ( 0 )
{
}
explicit Docement ( int );
};
Docement::Docement ( int var )
{
docid = var;
}
class Book : public Docement
{
public:
int bookid;
Book ( void ) : bookid ( 1 ) //违背1
{
}
};
int main ( void )
{
Book mybook;
return ( 0 );
}
#include <iostream>
using namespace std;
class Docement
{
public:
int docid;
Docement ( void ) : docid ( 0 )
{
}
explicit Docement ( int );
};
Docement::Docement ( int var )
{
docid = var;
}
class Book : public Docement
{
public:
int bookid;
Book ( void ) : Document ( 1 ), bookid ( 1 ) //遵循1
{
}
};
int main ( void )
{
Book mybook;
return ( 0 );
}
#include <iostream>
using namespace std;
class B
{
public:
B ( void );
~B ( void ); //违背1
virtual void f ( int a ) = 0;
protected:
B ( const B & );
B &operator = ( const B & );
};
B::B ( void )
{
}
B::~B ( void )
{
}
class D : public B
{
public:
D ( void );
explicit D ( int a );
~D ( void ); //违背2
virtual void f ( int a );
private:
int *md;
D ( const D & );
D &operator = ( const D & );
};
D::D ( int a ) : B ( ), md ( new int )
{
*md = a;
}
void D::f ( int a )
{
*md = a;
}
D::~D ( void )
{
delete md;
md = NULL;
}
int main ( void )
{
B *d = new D ( 1 );
d->f ( 2 );
delete d;
d = NULL;
return ( 0 );
}
#include <iostream>
using namespace std;
class B
{
public:
B ( void );
virtual ~B ( void ); //遵循1
virtual void f ( int a ) = 0;
protected:
B ( const B & );
B &operator = ( const B & );
};
B::B ( void )
{
}
B::~B ( void )
{
}
class D : public B
{
public:
D ( void );
explicit D ( int a );
virtual ~D ( void ); //遵循2
virtual void f ( int a );
private:
int *md;
D ( const D & );
D &operator = ( const D & );
};
D::D ( int a ) : B ( ), md ( new int )
{
*md = a;
}
void D::f ( int a )
{
*md = a;
}
D::~D ( void )
{
delete md;
md = NULL;
}
int main ( void )
{
B *d = new D ( 1 );
d->f ( 2 );
delete d;
d = NULL;
return ( 0 );
}
#include <iostream>
using namespace std;
class Foo
{
public:
int a;
Foo ( void );
~Foo ( void );
};
Foo::Foo ( void )
{
a = 1;
}
Foo::~Foo ( void )
{
if ( 1 == a )
{
throw 0;
} //违背1
}
int main ( void )
{
try
{
Foo f;
}
catch ( int e )
{
//......
}
return ( 0 );
}
#include <iostream>
using namespace std;
class Foo
{
public:
int a;
Foo ( void );
~Foo ( void );
};
Foo::Foo ( void )
{
a = 1;
}
Foo::~Foo ( void )
{
try
{
if ( 1 == a )
{
throw 0;
}
}
catch ( int e ) //遵循1
{
//......
}
}
int main ( void )
{
try
{
Foo f;
}
catch ( int e )
{
//......
}
return ( 0 );
}
#include <iostream>
using namespace std;
class Base
{
public:
Base ( void );
virtual ~Base ( void );
virtual int g ( int a = 0 );
};
Base::Base ( void )
{
}
Base::~Base ( void )
{
}
int Base::g ( int a )
{
return ( a + 1 );
}
class Derived : public virtual Base
{
public:
Derived ( void );
virtual ~Derived ( void );
virtual int g ( int a = 1 ); //违背1
};
Derived::Derived ( void )
{
}
Derived::~Derived ( void )
{
}
int Derived::g ( int a )
{
return ( a + 11 );
}
int main ( void )
{
int i;
Derived d;
Base &b = d;
i = b.g ( );
i = d.g ( );
return ( 0 );
}
#include <iostream>
using namespace std;
class Base
{
public:
Base ( void );
virtual ~Base ( void );
virtual int g1 ( int a = 0 );
virtual int g2 ( int a = 0 );
};
Base::Base ( void )
{
}
Base::~Base ( void )
{
}
int Base::g1 ( int a )
{
return ( a + 1 );
}
int Base::g2 ( int a )
{
return ( a + 2 );
}
class Derived : public Base
{
public:
Derived ( void );
virtual ~Derived ( void );
virtual int g1 ( int a = 0 ); //遵循1
virtual int g2 ( int a ); //遵循2
};
Derived::Derived ( void )
{
}
Derived::~Derived ( void )
{
}
int Derived::g1 ( int a )
{
return ( a + 11 );
}
int Derived::g2 ( int a )
{
return ( a + 12 );
}
int main ( void )
{
int i, j;
Derived d;
Base &b = d;
i = b.g1 ( );
i = d.g1 ( );
j = b.g2 ( );
j = d.g2 ( 0 );
return ( 0 );
}
#include <iostream>
using namespace std;
class Base
{
public:
Base ( void );
virtual ~Base ( void );
virtual int g ( int a = 0 );
};
Base::Base ( void )
{
}
Base::~Base ( void )
{
}
int Base::g ( int a )
{
return ( a + 1 );
}
class Derived : public Base
{
public:
Derived ( void );
virtual ~Derived ( void );
int g ( int a = 0 ); //违背1
};
Derived::Derived ( void )
{
}
Derived::~Derived ( void )
{
}
int Derived::g ( int a )
{
return ( a + 2 );
}
int main ( void )
{
int i, j;
Derived d;
Base &b = d;
i = b.g ( );
j = d.g ( );
return ( 0 );
}
#include <iostream>
using namespace std;
class Base
{
public:
Base ( void );
virtual ~Base ( void );
virtual int g ( int a = 0 );
};
Base::Base ( void )
{
}
Base::~Base ( void )
{
}
int Base::g ( int a )
{
return ( a + 1 );
}
class Derived : public Base
{
public:
Derived ( void );
virtual ~Derived ( void );
virtual int g ( int a = 0 ); //遵循1
};
Derived::Derived ( void )
{
}
Derived::~Derived ( void )
{
}
int Derived::g ( int a )
{
return ( a + 2 );
}
int main ( void )
{
int i, j;
Derived d;
Base &b = d;
i = b.g ( );
j = d.g ( );
return ( 0 );
}
#include <iostream>
using namespace std;
class A
{
public:
A ( void );
virtual ~A ( void );
virtual void foo ( void ) = 0;
};
A::A ( void )
{
}
A::~A ( void )
{
}
class B : public A
{
public:
B ( void );
virtual ~B ( void );
virtual void foo ( void );
};
B::B ( void )
{
}
B::~B ( void )
{
}
void B::foo ( void )
{
}
class C : public B
{
public:
C ( void );
virtual ~C ( void );
virtual void foo ( void ) = 0; //违背1
};
C::C ( void )
{
}
C::~C ( void )
{
}
int main ( void )
{
B myb;
myb.foo ( );
return ( 0 );
}
#include <iostream>
using namespace std;
struct S
{
int i;
int j;
int k;
};
class C
{
public:
int i;
int j;
int k;
C ( void );
virtual ~C ( void );
};
C::C ( void ) : i ( 0 ), j ( 0 ), k ( 0 )
{
}
C::~C ( void )
{
}
int main ( void )
{
S *s = new S;
s->i = 0;
s->j = 0;
s->k = 0;
C *c = reinterpret_cast < C * > ( s ); //违背1
//...
return ( 0 );
}
#include <iostream>
using namespace std;
class A
{
public:
A ( void );
~A ( void );
explicit A ( int );
int ma;
};
A::A ( void )
{
ma = 0;
}
A::A ( int a )
{
ma = a;
}
A::~A ( void )
{
}
int main ( void )
{
A const a1 = A ( 10 );
A *a2 = const_cast < A * > ( &a1 ); //违背1
a2->ma = 11;
A &a3 = const_cast < A & > ( a1 ); //违背2
a3.ma = 12;
return (0);
}
#include <iostream>
using namespace std;
void fun1 ( void )
{
int *p = new int; //违背1
*p = 1;
//......
}
void fun2 ( void )
{
int *p = new int[ 3 ]; //违背2
p[ 0 ] = 1;
p[ 1 ] = 2;
p[ 2 ] = 3;
//......
}
int main ( void )
{
fun1 ( );
fun2 ( );
return ( 0 );
}
#include <iostream>
using namespace std;
void fun1 ( void )
{
int *p = new int;
*p = 1;
delete p; //遵循1
p = NULL;
}
void fun2 ( void )
{
int *p = new int[ 3 ];
p[ 0 ] = 1;
p[ 1 ] = 2;
p[ 2 ] = 3;
delete[ ] p; //遵循2
p = NULL;
}
int main ( void )
{
fun1 ( );
fun2 ( );
return ( 0 );
}
#include <iostream>
using namespace std;
void fun ( void )
{
int *p = new int[ 3 ];
p[ 0 ] = 1;
p[ 1 ] = 2;
p[ 2 ] = 3;
delete p; //违背1
p = NULL;
}
int main ( void )
{
fun ( );
return ( 0 );
}
#include <iostream>
using namespace std;
void fun ( void )
{
int *p = new int[ 3 ];
p[ 0 ] = 1;
p[ 1 ] = 2;
p[ 2 ] = 3;
delete[ ] p; //遵循1
p = NULL;
}
int main ( void )
{
fun ( );
return ( 0 );
}
#include <iostream>
using namespace std;
int fun ( void );
int main ( void )
{
int i;
i = fun ( );
return ( 0 );
}
int fun ( void )
{
int *p = new int[ 3 ];
if ( NULL == p )
{
return ( -1 );
}
else
{
*p = 1;
p++;
*p = 2;
delete[ ] p; //违背1
p = NULL;
}
return ( 0 );
}
#include <iostream>
using namespace std;
int fun ( void );
int main ( void )
{
int i;
i = fun ( );
return ( 0 );
}
int fun ( void )
{
int *p = new int[ 3 ];
int *pbak = p;
if ( NULL == p )
{
return ( -1 );
}
else
{
*p = 1;
p++;
*p = 2;
delete[ ] pbak; //遵循1
pbak = NULL;
}
return ( 0 );
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment