Expression To An Type-Id
Expression To An Type-Id
mov al, 2
keyword invokes
mov dx, 0xD007
the inline out dx, al
assembler and can }
appear wherever a
C or C++ __asm mov al, 2
statement is legal. __asm mov dx, 0xD007
__asm out dx, al
It cannot appear by
itself. It must be
followed by an
assembly
instruction, a
group of
instructions
enclosed in braces,
or, at the very
least, an empty
pair of braces. The
term "__asm
block" here refers
to any instruction
or group of
instructions,
whether or not in
braces.
Dynamic_cast Converts the // dynamic_cast_8.cpp
// compile with: /GR /EHsc
operand
#include <stdio.h>
expression to an #include <iostream>
object of type
type-id. struct A {
virtual void test() {
printf_s("in A\n");
}
};
struct B : A {
virtual void test() {
printf_s("in B\n");
}
void test2() {
printf_s("test2 in B\n");
}
};
struct C : B {
virtual void test() {
printf_s("in C\n");
}
void test2() {
printf_s("test2 in C\n");
}
};
void Globaltest(A& a) {
try {
C &c = dynamic_cast<C&>(a);
printf_s("in GlobalTest\n");
}
catch(std::bad_cast) {
printf_s("Can't cast to C\n");
}
}
int main() {
A *pa = new C;
A *pa2 = new B;
pa->test();
B * pb = dynamic_cast<B *>(pa);
if (pb)
pb->test2();
C * pc = dynamic_cast<C *>(pa2);
if (pc)
pc->test2();
C ConStack;
Globaltest(ConStack);
class D : B {
public:
using B::f;
using B::g;
void f(int) {
printf_s("In D::f()\n");
f('c');
}
void g(int) {
printf_s("In D::g()\n");
g('c');
}
};
int main() {
D myD;
myD.f(1);
myD.g('a');
}
Console::WriteLine(FloatPtr::typeid);
}
class CTest {
public:
CTest() {};
~CTest() {};
const char *ShowReason() const {
return "Exception in CTest
class.";
}
};
class CDtorDemo {
public:
CDtorDemo();
~CDtorDemo();
};
CDtorDemo::CDtorDemo() {
cout << "Constructing CDtorDemo.\n";
}
CDtorDemo::~CDtorDemo() {
cout << "Destructing CDtorDemo.\n";
}
void MyFunc() {
CDtorDemo D;
cout<< "In MyFunc(). Throwing CTest
exception.\n";
throw CTest();
}
int main() {
cout << "In main.\n";
try {
cout << "In try block, calling
MyFunc().\n";
MyFunc();
}
catch( CTest E ) {
cout << "In catch handler.\n";
cout << "Caught CTest exception
type: ";
cout << E.ShowReason() << "\n";
}
catch( char *str ) {
cout << "Caught some other
exception: " << str << "\n";
}
cout << "Back in main. Execution
resumes here.\n";
}
sizeOfBuffer =
strlen( otherbuf.buffer ) + 1;
buffer = new
char[sizeOfBuffer];
strcpy_s( buffer, sizeOfBuffer,
otherbuf.buffer );
}
return *this;
}
int main()
{
Buf myBuf( "my buffer", 10 );
Buf yourBuf( "your buffer", 12 );
// assignment opperator
myBuf = yourBuf;
int main()
{
A<B<22>>();
}
ch = static_cast<char>(i); // int
to char
dbl = static_cast<double>(f); //
float to double
i = static_cast<BYTE>(ch);
}
class Y : public X {
public:
void useProtfunc() { Protfunc(); }
} y;
int main() {
// x.m_protMemb; error,
m_protMemb is protected
x.setProtMemb( 0 ); // OK, uses
public access function
x.Display();
y.setProtMemb( 5 ); // OK, uses
public access function
y.Display();
// x.Protfunc(); error,
Protfunc() is protected
y.useProtfunc(); // OK, uses
public access function
// in derived
class
}
Default access of
members in a class
is private. Default
access of members
in a structure or
union is public.
private When preceding a // keyword_private.cpp
class BaseClass {
list of class
public:
members, the // privMem accessible from member
private keyword function
specifies that those int pubFunc() { return privMem; }
members are private:
void privMem;
accessible only };
from member
functions and class DerivedClass : public BaseClass {
friends of the public:
class. This applies void usePrivate( int i )
{ privMem = i; } // C2248:
to all members privMem not accessible
declared up to the // from
next access derived class
specifier or the end };
of the class. class DerivedClass2 : private BaseClass
{
When preceding public:
the name of a base // pubFunc() accessible from derived
class, the private class
int usePublic() { return
keyword specifies pubFunc(); }
that the public and };
protected members
of the base class int main() {
are private BaseClass aBase;
DerivedClass aDerived;
members of the DerivedClass2 aDerived2;
derived class. aBase.privMem = 1; // C2248:
privMem not accessible
aDerived.privMem = 1; // C2248:
privMem not accessible
// in
derived class
aDerived2.pubFunc(); // C2247:
pubFunc() is private in
// derived
class
}
c = a + b;
c.Display();
}
System::Console::WriteLine("D::f()
called");
}
System::Console::WriteLine("D::g()
called");
}
};
System::Console::WriteLine("E::f()
called");
}
};
int main() {
D^ d = gcnew D;
C^ c = gcnew D;
D ^ e = gcnew E;
e->f(); // calls E::f
}
class MyClass {
public:
void print() { cout << i << ' '; }
// Implicitly inline
private:
int i;
};
friend In some
1. friend class-name;
circumstances, it is
friend function-declarator;
more convenient to
grant member- 2. #include
level access to class exforsys
functions that are {
not members of a private:
class or to all int a,b;
public:
functions in a
void test()
separate class. {
a=100;
The friend b=200;
keyword allows a }
friend int compute(exforsys e1)
function or class to
gain access to the
//Friend Function Declaration with keyword friend and
private and with the object of class exforsys to which it is friend
protected members passed to it
of a class. };
main()
{
exforsys e;
e.test();
cout<<"The result is:"<
//Calling of Friend Function with object as argument.
}
C()
{
i = 0;
}
};
class C2
{
public:
int i;
explicit C2(int i ) // an
explicit constructor
{
}
};
C f(C c)
{ // C2558
c.i = 2;
return c; // first call to copy
constructor
}
void f2(C2)
{
}
void g(int i)
{
f2(i); // C2558
// try the following line instead
// f2(C2(i));
}
int main()
{
C c, d;
d = f(c); // c is copied
}
Const_cast Removes the // expre_const_cast_Operator.cpp
// compile with: /EHsc
const, volatile,
#include <iostream>
and __unaligned
attribute(s) from a using namespace std;
class. class CCTest {
public:
void setNumber( int );
A pointer to any void printNumber() const;
object type or a private:
pointer to a data int number;
member can be };
explicitly
void CCTest::setNumber( int num )
converted to a type { number = num; }
that is identical
except for the void CCTest::printNumber() const {
const, volatile, cout << "\nBefore: " << number;
const_cast< CCTest * >( this )-
and __unaligned >number--;
qualifiers. For cout << "\nAfter: " << number;
pointers and }
references, the
result will refer to int main() {
CCTest X;
the original object. X.setNumber( 8 );
For pointers to X.printNumber();
data members, the }
result will refer to
the same member
as the original
(uncast) pointer to
data member.
class The class keyword // class.cpp
// compile with: /EHsc
declares a class
// Example of the class keyword
type or defines an // Exhibits polymorphism/virtual
object of a class functions.
type.
#include <iostream>
#include <string>
#define TRUE = 1
using namespace std;
class dog
{
public:
dog()
{
_legs = 4;
_bark = true;
}
private:
string _dogSize, _earType;
int _legs;
bool _bark;
};
string getColor()
{
return _color;
}
protected:
string _color, _earLength, _earType;
};
int main()
{
dog mongrel;
breed labrador("yellow", "large");
mongrel.setEars("pointy");
labrador.setEars("long", "floppy");
cout << "Cody is a " <<
labrador.getColor() << " labrador" <<
endl;
}
class CTest {
public:
CTest() {};
~CTest() {};
const char *ShowReason() const {
return "Exception in CTest
class.";
}
};
class CDtorDemo {
public:
CDtorDemo();
~CDtorDemo();
};
CDtorDemo::CDtorDemo() {
cout << "Constructing CDtorDemo.\n";
}
CDtorDemo::~CDtorDemo() {
cout << "Destructing CDtorDemo.\n";
}
void MyFunc() {
CDtorDemo D;
cout<< "In MyFunc(). Throwing CTest
exception.\n";
throw CTest();
}
int main() {
cout << "In main.\n";
try {
cout << "In try block, calling
MyFunc().\n";
MyFunc();
}
catch( CTest E ) {
cout << "In catch handler.\n";
cout << "Caught CTest exception
type: ";
cout << E.ShowReason() << "\n";
}
catch( char *str ) {
cout << "Caught some other
exception: " << str << "\n";
}
cout << "Back in main. Execution
resumes here.\n";
}
bool This keyword is a // bool.cpp
#include <stdio.h>
built-in type. A
variable of this int main()
type can have {
values true and #if !defined(__BOOL_DEFINED)
false. Conditional printf_s("bool is not
supported\n");
expressions have #elif defined(__BOOL_DEFINED)
the type bool and printf_s("bool is supported\n");
so have values of #endif
type bool. }
while Executes statement // while_statement.cpp
repeatedly until
#include <string.h>
expression evaluates #include <stdio.h>
to zero. char *trim( char *szSource )
{
The test of char *pszEOS = 0;
expression takes // Set pointer to character
place before each before terminating NULL
execution of the pszEOS = szSource +
loop; therefore, a strlen( szSource ) - 1;
while loop executes
// iterate backwards until non
zero or more times. '_' is found
expression must be while( (pszEOS >= szSource) &&
of an integral type, a (*pszEOS == '_') )
pointer type, or a *pszEOS-- = '\0';
class type with an return szSource;
unambiguous }
conversion to an int main()
integral or pointer {
type. char szbuf[] = "12345_____";
int main() {
HANDLE hThread1, hThread2;
DWORD retCode;
hThread1 = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)&ThreadFunc1,
NULL, 0, NULL);
hThread2 = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)&ThreadFunc2,
NULL, 0, NULL);
retCode =
WaitForSingleObject(hThread1,3000);
CloseHandle(hThread1);
CloseHandle(hThread2);