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

main

parents
int main ( void )
{
float f = 0.0, g = 1.0;
for ( f = 0.0; f < 10.0; f = f + 1.0 ) //违背1
{
g = f + g;
}
return ( 0 );
}
int main ( void )
{
int i, j, k;
j = 100;
k = 0;
for ( i = 0; i < j; i++ )
{
i = 2 * i; //违背1
k = k + 1;
}
return ( 0 );
}
int main ( void )
{
int i, j, k;
j = 100;
k = 0;
for ( i = 0; i < j; i++ )
{
i = 2 * i; //违背1
k = k + 1;
}
return ( 0 );
}
int Gstatic = 0;
int main ( void )
{
for ( ; ; ) //违背1
{
//...
if ( 1 == Gstatic )
{
break;
}
}
return ( 0 );
}
int Gstatic = 0;
int main ( void )
{
for ( ; ; ) //违背1
{
//...
if ( 1 == Gstatic )
{
break;
}
}
return ( 0 );
}
int Gstatic = 0;
int main ( void )
{
while ( 1 ) //遵循1
{
//...
if ( 1 == Gstatic )
{
break;
}
}
return ( 0 );
}
int Gstatic = 0;
int main ( void )
{
while ( 1 ) //遵循1
{
//...
if ( 1 == Gstatic )
{
break;
}
}
return ( 0 );
}
#include <iostream>
using namespace std;
class B
{
public:
B ( void );
virtual ~B ( void );
virtual int g ( int a = 0 );
private:
int b;
};
B::B ( void ) : b ( 1 )
{
}
B::~B ( void )
{
}
int B::g ( int a )
{
return ( a + b );
}
class D : public virtual B
{
public:
D ( void );
virtual ~D ( void );
virtual int g ( int a = 0 );
private:
int d;
};
D::D ( void ) : B ( ), d ( 2 )
{
}
D::~D ( void )
{
}
int D::g ( int a )
{
return ( a + d );
}
int main ( void )
{
D d;
B &b = d;
B *pb = &d;
D *pd1 = reinterpret_cast < D * > ( pb ); //违背1
D &pd2 = reinterpret_cast < D & > ( *pb ); //违背2
return ( 0 );
}
#include <iostream>
using namespace std;
class B
{
public:
B ( void );
virtual ~B ( void );
virtual int g ( int a = 0 );
private:
int b;
};
B::B ( void ) : b ( 1 )
{
}
B::~B ( void )
{
}
int B::g ( int a )
{
return ( a + b );
}
class D : public virtual B
{
public:
D ( void );
virtual ~D ( void );
virtual int g ( int a = 0 );
private:
int d;
};
D::D ( void ) : B ( ), d ( 2 )
{
}
D::~D ( void )
{
}
int D::g ( int a )
{
return ( a + d );
}
int main ( void )
{
D d;
B &b = d;
B *pb = &d;
D *pd1 = dynamic_cast < D * > ( pb ); //遵循1
D &pd2 = dynamic_cast < D & > ( *pb ); //遵循2
return ( 0 );
}
#include <iostream>
using namespace std;
class A
{
public:
A ( void );
void SetA ( int );
private:
int a;
};
A::A ( void ) : a ( 0 )
{
}
void A::SetA ( int va )
{
a = va;
}
class B1 : public A //违背1
{
public:
B1 ( void );
void SetB1 ( int );
private:
int b1;
};
B1::B1 ( void ) : A ( ), b1 ( 0 )
{
}
void B1::SetB1 ( int vb )
{
b1 = vb;
SetA ( b1 + 1 );
}
class B2 : public A //违背2
{
public:
B2 ( void );
void SetB2 ( int );
private:
int b2;
};
B2::B2 ( void ) : A ( ), b2 ( 0 )
{
}
void B2::SetB2 ( int vb )
{
b2 = vb;
SetA ( b2 + 2 );
}
class D : public B1, public B2
{
public:
D ( void );
private:
int d;
};
D::D ( void ) : B1 ( ), B2 ( ), d ( 0 )
{
}
int main ( void )
{
D thed;
thed.SetB1 ( 1 );
thed.SetB2 ( 2 );
return ( 0 );
}
#include <iostream>
using namespace std;
class A
{
public:
A ( void );
void SetA ( int );
private:
int a;
};
A::A ( void ) : a ( 0 )
{
}
void A::SetA ( int va )
{
a = va;
}
class B1 : public virtual A //遵循1
{
public:
B1 ( void );
void SetB1 ( int );
private:
int b1;
};
B1::B1 ( void ) : A ( ), b1 ( 0 )
{
}
void B1::SetB1 ( int vb )
{
b1 = vb;
SetA ( b1 + 1 );
}
class B2 : public virtual A //遵循2
{
public:
B2 ( void );
void SetB2 ( int );
private:
int b2;
};
B2::B2 ( void ) : A ( ), b2 ( 0 )
{
}
void B2::SetB2 ( int vb )
{
b2 = vb;
SetA ( b2 + 2 );
}
class D : public B1, public B2
{
public:
D ( void );
private:
int d;
};
D::D ( void ) : B1 ( ), B2 ( ), d ( 0 )
{
}
int main ( void )
{
D thed;
thed.SetB1 ( 1 );
thed.SetB2 ( 2 );
return ( 0 );
}
#include <iostream>
using namespace std;
class B
{
public:
B ( void );
virtual ~B ( void );
virtual void f ( void ) = 0;
B &operator = ( B const &rhs ); //违背1
private:
int kind;
};
B::B ( void ) : kind ( 0 )
{
}
B::~B ( void )
{
}
B & B::operator = ( B const &rhs )
{
kind = rhs.kind;
return ( *this );
}
class D : public B
{
public:
D ( void );
virtual ~D ( void );
virtual void f ( void ) { };
D &operator = ( D const &rhs );
private:
int member;
};
D::D ( void ) : B ( ), member ( 0 )
{
}
D::~D ( void )
{
}
D & D::operator = ( D const &rhs )
{
member = rhs.member;
return ( *this );
}
int main ( void )
{
D d1;
D d2;
B &b1 = d1;
B &b2 = d2;
b1 = b2; //违背2
return ( 0 );
}
#include <iostream>
using namespace std;
class B
{
public:
B ( void );
virtual ~B ( void );
virtual void f ( void ) = 0;
protected:
B &operator = ( B const &rhs ); //遵循1
private:
int kind;
};
B::B ( void ) : kind ( 0 )
{
}
B::~B ( void )
{
}
B & B::operator = ( B const &rhs )
{
kind = rhs.kind;
return ( *this );
}
class D : public B
{
public:
D ( void );
virtual ~D ( void );
virtual void f ( void ) { };
D &operator = ( D const &rhs );
private:
int member;
};
D::D ( void ) : B ( ), member ( 0 )
{
}
D::~D ( void )
{
}
D & D::operator = ( D const &rhs )
{
member = rhs.member;
return ( *this );
}
int main ( void )
{
D d1;
D d2;
B &b1 = d1;
B &b2 = d2;
b1 = b2; //由于采取了保护,此用法被禁止,编译报错
return ( 0 );
}
#include <iostream>
using namespace std;
int gVar = 10;
class Foo
{
public:
Foo ( void );
~Foo ( void );
private:
int a;
};
Foo::Foo ( void )
{
a = gVar; //违背1
}
Foo::~Foo ( void )
{
}
int main ( void )
{
Foo thef;
return ( 0 );
}
#include <iostream>
using namespace std;
int gVar = 10;
class Foo
{
public:
Foo ( void );
explicit Foo ( int );
~Foo ( void );
private:
int a;
};
Foo::Foo ( void )
{
a = 0;
}
Foo::Foo ( int var )
{
a = var;
}
Foo::~Foo ( void )
{
}
int main ( void )
{
Foo thef ( gVar ); //遵循1
return ( 0 );
}
#include <iostream>
using namespace std;
class Foo //违背1
{
public:
~Foo ( void );
void SetVer ( int );
private:
int a;
};
Foo::~Foo ( void )
{
}
void Foo::SetVer ( int var )
{
a = var;
}
int main ( void )
{
Foo thef;
thef.SetVer ( 2 );
return ( 0 );
}
#include <iostream>
using namespace std;
class Foo
{
public:
Foo ( void ); //遵循1
~Foo ( void );
void SetVer ( int );
private:
int a;
};
Foo::Foo ( void )
{
a = 0;
}
Foo::~Foo ( void )
{
}
void Foo::SetVer ( int var )
{
a = var;
}
int main ( void )
{
Foo thef;
thef.SetVer ( 2 );
return ( 0 );
}
#include <iostream>
using namespace std;
class Foo
{
public:
Foo ( void );
Foo ( int var ); //违背1
int getVar ( void );
~Foo ( void );
private:
int a;
};
Foo::Foo ( void )
{
a = 0;
}
Foo::Foo ( int var )
{
a = var;
}
int Foo::getVar ( void )
{
return a;
}
Foo::~Foo ( void )
{
}
void f ( Foo );
int main ( void )
{
int i = 1;
f ( i );
return ( 0 );
}
void f ( Foo thef )
{
int j;
//...
j = thef.getVar ( );
}
#include <iostream>
using namespace std;
class Foo
{
public:
Foo ( void );
explicit Foo ( int var ); //遵循1
int getVar ( void );
~Foo ( void );
private:
int a;
};
Foo::Foo ( void )
{
a = 0;
}
Foo::Foo ( int var )
{
a = var;
}
int Foo::getVar ( void )
{
return a;
}
Foo::~Foo ( void )
{
}
void f ( int );
int main ( void )
{
Foo thef ( 1 );
int i = 1;
f ( i );
return ( 0 );
}
void f ( int i )
{
int j;
Foo thef ( 1 );
//...
j = thef.getVar ( );
}
#include <iostream>
using namespace std;
class Foo
{
public:
int a;
Foo ( void );
explicit Foo ( int );
private:
int b;
};
Foo::Foo ( void ) //违背1
{
a = 0;
}
Foo::Foo ( int varb ) //违背2
{
b = varb;
}
int main ( void )
{
Foo thef1, thef2 ( 1 );
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