The world of C++

[PDF Copy]

Defining and using a class

Header:

// A.h

#ifndef A_HEADER

#define A_HEADER

class A {

public:

// Exposed member functions or Methods

void foo1(); // Declaration

// Inlined function: foo2

void foo2() {

}

private:

// Private member functions

void foo3();

// Data member(s)

int data;

};

#endif

Source:

#include “A.h”

void A::foo2() { … }

void A::foo3() { … }

Usage:

Static allocation of object:

{

A a;  // Default constructor invoked

a.foo2();

a.foo3(); // Error: Private member function

} // Destructor for a invoked

Dynamic allocation of object:

A* a = new A(); // Default constructor invoked

// Use a

a->foo1();

a->foo2();

delete a; // Destructor invoked

A* a = new A[n]; // Calls the default constructor n times

delete [] a;

4 default methods of a class

class A{

public:

// No member functions defined

private:

int mInt;           // Data member of type int

int* mPointerToInt; // Data member of type pointer to int

B mb;                // Data member of type class B

};

This is equivalent to the following:

class A {

public:

// Default constructor (can be overwritten or overloaded)

A() : mb() {

// mb is initialized through its constructor

// No initialization for data member of type int

// No initialization for data member of type int*

}

// Default destructor (can be overwritten)

~A() {

mb.~B();

}

// Default copy constructor

A(const A& other) : mb(other.mb) {

mInt = other.mInt;

mPointerToInt = other.mPointerToInt;

}

// Default assignment operator

A& operator=(const A& other) {

mInt = other.mInt;

mPointerToInt = other.mPointerToInt;

mb(other.mb);

return *this;

}

};

Need for overwriting the 4 default methods

Data member type Need to overwrite constructor Need to overwrite destructor Need to overwrite copy constructor Need to overwrite assignment operator
Non-pointer built-in (int, float, double, char, bool) Yes (for initialization) No No No
Pointer Yes (for allocation) Yes (for deallocation) Yes (for deep copy) Yes (for deep copy)
Class No No No No

 

Const-correctness

Naming values

const float g = 9.8;

g = … // Error: Illegal to modify

const -> property of the path, not of the variable

Example:

A a;

a = …; // Fine

foo(a);

void foo(const A& b) {

b = … // Error

}

Const Member function -> Member variables cannot be modified by this function

Class A {

Public:

void foo() const; // equivalent to void foo(A* const this);

};

Since const applies to the “this” pointer (which is a pointer to the self-object), member variables cannot be modified.

Choosing function interface

Speed?(copy constructor is not invoked) Is it safe?(Callee cannot modify the caller’s actual parameter) Can it be NULL?(to indicate the absence of a value)
Void A(      B  b) Slow Yes No
Void A(      B* b) Fast No Yes
Void A(const B* b) Fast Yes Yes
Void A(      B& b) Fast No No
Void A(const B& b) Fast Yes No

 

Namespaces

// A.h

class foo { … };

// B.h

class foo { … };

// use.cpp

#include “A.h”

#include “B.h” // Clash of class definitions!

Solution:

// A.h

namespace A { // Global scoping

class foo { … };

}

// B.h

namespace B { // Global scoping

class foo { … };

}

// use.cpp

#include “A” // No suffix “.h” when the header contains a namespace

#include “B” // No clash!

A::foo objectFoo1;

B::foo objectFoo2;

Operator overloading

(x + y) is equivalent to

x.operator+(y) // Member function

OR

operator+(x,y) // Non-member function

Chaining of operators is also possible:

x + y + z is equivalent to ((x + y) + z)

OR (x.operator+(y)).operator+(z)

Signature:

A operator+(const A& other);

x = y = z is equivalent to x = (y = z)

OR x.operator=(y.operator=(z))

Signature:

A& operator=(const A& other);

std::cin >> x >> y is equivalent to

operator>>(operator>>(std::cin, x), y)

Signature (must be a non-member function):

std::istream& operator>>(std::istream& s, A& a)

std::cout << x << y is equivalent to

operator<<(operator<<(std::cout, x), y)

Signature (must be a non-member function):

std::ostream& operator<<(std::ostream& s, const A& a)

Prefix/postfix operators:

++x is equivalent to x.operator++()

x++ is equivalent to x.operator++(0) // 0 is irrelevant but used for function overloading


++(++x) is equivalent to x.operator++().operator++().

(x++)++ is NOT legal.

A& operator++() // Prefix

{

// Increment logic

return *this;

}

A operator++(int) // Postfix

{

A result(*this); // Copy constructor called

++(*this);  // Prefix operator function invoked

return result;

}

Subscripting:

A a;

MemberType m;

a[i] = m; // equivalent to a.operator[](i) = m

const A a;

a[i] = m; // illegal

Signatures:

const MemberType& operator[](int index) const; // Receiver is a const

MemberType& operator[](int index); // Receiver is a not const

 

This entry was posted in C++. Bookmark the permalink.

Comments are closed.