C++ QuickGuide PDF
C++ QuickGuide PDF
v 0.91
Adam Grossman <[email protected]>
Class Definition
<type> operator<operator>([argument]);
};
class declaration
Field Purpose Value/Description
<class name> name of class any legal variable name
<access level1> controls access level of base public: access levels from base class remain the same
class to anything the uses protected: public members become protected
derived class private: public and protected members become private
class definition
access
Field Purpose Value/Description
<access level2> controls member access for There can be as many access level sections in the class as needed,
anything using class even the multiple sections of the same access level. The deffault
level is private.
constructors
NOTE: A base class's constructor is always called before the derived class's constructor. If the base class needs an
arguments to the constructor, include the parent class constructor in the initialization list
NOTE: If the constructor's single argument is of the same type as the class, and the constructor uses that object to make a
copy of it for the newly created object, it's called a copy constructor. If the single argument is of a type other then the class
itself, and it is used to convert the argument type to a new object of the constructor's class type, it's called a conversion
constructor.
friend
Field Purpose Value/Description
friend <class allows outside classes access gives the class name <class name> access to the member,
name> [<member>] to class regardless of it's access level (if the member is public, it has no
effect). If no member is given, the <class name> has access to all
of the class's private and protected members
operator overloading
This allows operators to be overloaded to common operators (+,-,[], etc) can be used in a class, hopefully to make the class
more intuitive for the user of the class. For binary operators, it's the left side operand which makes the call. For unary
operators, the object that makes the call is based on the operator type.
return temp_obj;
}
destructor
NOTE: destructors are called from the derived class first, then the base class
Field Purpose Value/Description
virtual allows polymorphism behaves just like a virtual method,
except the base class destructor will be
called. But if the destructor is not
declared virtual, and the class is
destroyed via a pointer to the base
class, the derived class's destructor will
not be called.
~<class name>() cleans up class destructors are always the same name
as the class and never has any
arguments
Method overloading/overriding
Methods can either can either be overloaded (used for methods with common name in the same class) or overridden (used in
inheritance). Overloading/overriding is based the methods signature, which is the methods name and arguments. The return
value is not part of the methods signature.
Overloading Methods in the same class share the same name, but have different arguments.
Overridding Method has the same signature has a method in it's base class. The method in the derived class
will be called. If the base call and derived class have the same name, but different arguments, it
is functionally similar to having overloaded methods in the derived class
iostream
stdin. stdout, stderr
writing to stdout/stderr stream:
#include <iostream>
cin
read in data from stdin
#include <iostream>
NOTE: std::cin's return value evaluates to false in two situations. It read an EOF or the incoming stream contains date
that is not a legal value for variable.
File I/O
There are three file stream objects
All three basically behave the same, just and are more restrictive. This allows a stream object to be passed to a method
while having compile time checking to make sure the right stream direction object is being passed.
opening a file
NOTE: The arguments in File.open can also be used for the *fstream constructor.
NOTE: Only certain modes are valid for certain file stream objects
#include <fstream>
[fstream|ifstream|ofstream] File;
Mode Description
ios::in Open file to read
ios::out Open file to write
ios::app All the data you write, is put at the end
of the file. It calls ios::out
ios::ate All the date you write, is put at the end
of the file. It does not call ios::out
ios::trunc Deletes all previous content in the file.
(empties the file)
ios::binary Opens the file in binary mode.
close file:
<fstream object>..close();
(do not think there is a need to elaborate)
input methods
File >> <value>; //reads one word at a time, does include EOL
File.getch(<char>); //reads one char at time
File.getline(char *string,int length); //reads max at least length chars
output method
File << <value>; //reads one word at a time, does include EOL
File.putch(<char>); //write one char at time
File.write(char *string,int length); //reads max at least length chars
Casting
All casts are in the form of:
*_cast<<target_type>>(<source expression>)
Const Handling
Basically ‘const’ applies to whatever is on its immediate left (other than if there is nothing there in which case it applies to
whatever is its immediate right).
Example Description
const int * Constant2 Declare that Constant2 is variable pointer to a constant
int const * Constant2 integer
int * const Constant3 Declare that Constant3 is constant pointer to a variable
integer
int const * const Constant4 Declare that Constant4 is constant pointer to a constant
integer.
class A Adding a const to the end of a method guarantees that no
{ member variables will be altered in the function call.
int func() const;
}
Templates
Mechanisms for generic types. The entire template (declaration and implementation) must be inside a header file, because it
requires a recompilation every time it is used, unlike a standard library
Function Template
Standard function overloading applies, including overloading between template and non-template functions.
int i,j;
char x,y;
i=1;
x=1;
Class Template
T1 getX();
void setY(T2 inY);
};
template <class T1, class T2> T1 classA<T1,T2>::getX()
{ return x;
}
x=c.getX();
c.setY(1.02);
Specialization
Allows the overriding of the default template implementation for a certain type. In a specialized template, the same
methods do not need to be implemented, each can have it's own set of methods, but specialized class will not inherit any
undefined methods from the generic template.
try
{
[throw <object> | function with throw <object>]
}
(catch (<[...| type variable]>)
{
})+
Rethrowing an exception
If a catch does not want to handle an exception or wants to have the next level also handle the exception, it can be
rethrown , simply by calling throw with no argument. The exception will be thrown down to the previous level. In this
example, the catch with the int argument in main will actually handle the exception.
Rethrowing example
int level_2()
{
std::cout << "Level 2" << std::endl;
throw 10;
int level_1()
{
std::cout << "Level 1" << std::endl;
try
{
level_2();
}
catch (char c)
{
std::cout << "L1: Error type char: " << c << std::endl;
}
catch (...)
{
throw;
}
}
int main()
{
try
{
level_1();
}
catch (int i)
{
std::cout << "Main: Error type int: " << i << std::endl;
}
catch (char c)
{
std::cout << "Main: Error type default" << std::endl;
}
Exception Specifier
Function specifiers specify what type of exceptions (if any) a function, or a function it calls can throw. A function without a
function specifier can throw any kind of exception.
NOTE: the exception specifier is not part of the function signature
function throw([types,]*)
Standard Exceptions
There are some standard exception classes which are used by the C++ Standard Library. They are derived from the
exception base class.
Document Changes