C++ Notes: Basics
C++ Notes
Just some C/C++ code snippets to keep in mind. C/C++ is tremendous complicated, but it’s still the most powerful programming language.
Table of Contents
- char to Int
- Pointer
- Pointer and Smart Pointer
- Array as Argument
- Operator that can’t be overloaded
- Object Instantization
- Object Relationship
- Virtual Function and Ploymorphism
- Friend
- Const
- Constexpr
- Extern and static
Char to Int
1. Char
sotred as ASCII
C store Char
as ASCII
(Int) by default. So, Char
is equal to ASCII code.
|
|
2. Char
to Int
, to String
, and vice versa
char
andint
1 2 3 4 5
char c='5' int res = c -'0' ; // 5 int i=5; char res = I + '0'; // '5'
char*
,int
andstring
1 2 3 4 5 6 7 8 9 10 11 12
// char * to string const char * str_c = "hello"; std::string str = str_c; // string to char* str.c_str(); // return const char* // int to string std::to_string() // string to int std::stoi()
3. Char
as subscript of array: legal!
This is useful when create hashmap
. e.g. counting chars
|
|
Operator Could not be Overloaded
operator | memo |
---|---|
. | Member access or dot operator |
.* | Pointer-to-member Operator |
:: | Scope Resolution operator |
? : | conditional operator, ternary operator |
sizeof | object size operator, built-in operations |
typeid | object type operator, built-in operations |
OK, What’s .*
?
|
|
Now, you can’t use x.somePointer(), or px->somePointer() because there is no such member in class X.
see here
Pointer
Pointer syntax
Rule: read from right to left
|
|
Declare two pointers
|
|
Pointer and Smart Pointer
|
|
Array
Array as formal arguments
An Array could not copy to anther Array (Copy pointer is not allowed!), so call-by-value
is not allowed.
So, use array pointer:
|
|
multi-dimension array
|
|
When use pointer to an Array, the dimension is unknown. So, need an extra argument to specify it explicitly.
Example:
|
|
Object Instantization
1. without new
stack
|
|
2. with new
heap
|
|
3. copy constructor
|
|
4. Smart Pointer
|
|
Friend
The friend
declaration appears in a class body
and grants a function
or another class
access to private
and protected
members of the class where the friend declaration appears.
1. friend function
Declare anywhere inside a class, but define outside
|
|
Example:
|
|
2. friend class
Delare inside class, define outside
|
|
Note: friend class X {};
is an error
Example:
|
|
3. Others: friend ostream, friend template …
|
|
Object Relationships
relation types
- “is-a”
- “has-a”
- “uses-a”
- “depends-on”
Property | Composition | Aggregation | Association | |
---|---|---|---|---|
Relationship type | Whole/part | Whole/part | Otherwise unrelated | |
Members can belong to multiple classes | No | Yes | Yes | |
Members existence managed by class | Yes | No | No | |
Directionality | Unidirectional | Unidirectional | Unidirectional or bidirectional | |
Relationship verb | Part-of | Has-a | Uses-a |
Composition: has a data member
Building complex objects from simpler ones is called object composition .
object composition models a “has-a” relationship between two objects. In C++, It means structs and classes can have data members of various types.
|
|
Summary:
- Typically use normal member variables
- Can use pointer members if the class handles object allocation/deallocation itself
- Responsible for creation/destruction of parts
Aggregation: “has a”
Unlike a composition, parts can belong to more than one object at a time, and the whole object is not responsible for the existence and lifespan of the parts.
Summary:
- Typically use pointer or reference members that point to or reference objects that live outside the scope of the aggregate class
- Not responsible for creating/destroying parts
Association: “uses a”
Association models as “uses-a” relationship. The doctor “uses” the patient (to earn income). The patient uses the doctor (for whatever health purposes they need).
Delegate: “has a”
or called pImpl(Pointer to IMPLementation)
Delegate: Composition by reference
has a pointer of another object
|
|
Inheritance: “is a”
public, protected, private
|
|
Virtual Functions and Runtime Polymorphism
Declare: vitrual keyword
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class TestA { public: virtual void func() { cout << "virtual function" << endl; } }; class Test : public TestA { public: virtual void func() { // virtual could be omited cout << "Test virtual function" << endl; } ~Test() { } }; TestA* t = new Test; // parent pointer point to child (Ploymorphism) t->func(); // Results:Test virtual function delete t;
Member Could not be virtual
- inline function
- constructor
- non-member function
- static function: only one copy of all objects.
- friend function: it’s non-member function
- member function template !
Pure virtual function
- declare
1
virtual void fun() = 0;
- class with pure virtual functoin could not be instantized!
- a derived class of virtual class has to define pure virtual function. then the derived class could be instantized.
abstract class
: class with pure virtual function
- declare
virtual deconstrutor
- A parent pointer point to it’s child. When delete the parent pointer, only parent constuctor is called. if declared a virtual deconstuctor, child’s deconstuctor is called first, then the parent deconsturctor.
- virtural keyword could be omited if a parent deconstructor is declared.
delete
a pointer will only called object’s deconstructor where the pointer point to.
see also Pointer and smart pointer cast
Const
1. const
before or behind type/class, the syntax semantic are same
|
|
2. Pointer with const: const int* p
, int const* p
and int *const p
Dirty trick: use *
as a separator, const
restrict the type according to the side where it belong to
point to const: These two expression are same
|
|
const pointer: But these two not the same
|
|
3. class member func
with const: () const
a. const object
- could not change class variable
- could not call
non-const
function
|
|
b. () const
- could not change class variable, except static
- could get variable
|
|
Easy to understand, when pointer this
is const
|
|
c. () const
overloading
constexpr
The constexpr specifier declares
that it is possible to evaluate the value
of the function or variable at compile time
.
default
and delete
in class
special class member:
- default constructor
- deconstructor
- copy constructor
- operater
=
when use default and delete
- default
|
|
- delete: prohibit func call marked by
delete
|
|
extern, static
global variable
- defined outside all functions and available to all functions.
- unaffected by scopes and are always available (exists until the program ends)
extern
- declare a
global variable
(exists on the whole project): variable could be used inmulti- .cpp
files extern "C" {/* c code */}
: compile c code.
static
- declare a
local global variable
(file scope): only be accessed in its translation unit or.o file
, that’s, in the file where it is created. - declare a static class member (class scope): initialization should be
outside class body
- static data member
- static function:
- no
this
pointer: only access to other static member/function - could declare as private
- no