C++ Notes

C vs. C++ Functions and Structs

Functions in C and C++

While functions in C and C++ are fundamentally similar, C++ provides additional functionalities such as true pass-by-reference and default arguments.

Pass-by-Reference

C: Pass-by-reference is not directly possible in C. Instead, it is emulated using pointers.

void foo(int *param){ /* Passing pointer by value */
    *param = 7; /* Dereference pointer to "emulate" reference and modify the value stored at param */
}

int Value = 3;
foo(&Value); // Pass memory address of Value

C++: C++ introduces a reference type, enabling true pass-by-reference.

void foo(int &Param){ // & passes a reference to Param instead of a copy
    // True pass by reference
    Param = 7;
}

int Value = 3;
foo(Value); // Directly pass Value

Default Arguments (C++ Only)

C++ allows functions to have default arguments, which simplifies function calls by providing default values for parameters.

// The default argument for param is 0 in function declaration
int foo(int param = 0);

// The default argument is not repeated in the function definition
int foo(int param){
    return param + 3;
}

// Call location can provide the argument or not
int x = foo(6); // x will be 9
x = foo();      // x will be 3, equivalent to foo(0)

Structs in C and C++

Structs in C++ are similar to those in C. However, C++ (especially from C++11 onwards) provides enhanced capabilities for structs.

Struct Definition

C: Structs are defined and used with typedef.

struct NAME_TAG{
    int DataMember;
};

// Required to just use Name
typedef struct NAME_TAG Name;

C++: Structs can be defined more succinctly.

struct Name{
    int DataMember;
};

Struct Instantiation and Initialization

Both C and C++ allow instantiation and initialization of structs, but C++ provides additional features.

Instantiation Example

// C
/* Assume Name is defined as above */
Name MyName;

// C++
// Assume Name is defined as above
Name MyName;

Simple Initialization Example

// C
/* Assume Name is defined as above */
Name MyName = {4};

// C++
// Assume Name is defined as above
Name MyName = {4};

Named Member Initialization (Designated Initializers)

Designated initializers allow you to initialize specific members of a struct.

// C
Name MyName = {.DataMember = 4};

// C++ (C++20)
Name MyName = {.DataMember = 4};

Default Initialization (C++ Only)

C++ allows default initialization of struct members, simplifying the initialization process.

struct Name{
    int DataMember = 4; // C++11
};

Name MyName; // DataMember will have value of 4

Struct Functions

Both C and C++ allow functions to operate on structs, but C++ supports member functions directly within the struct definition.

C Struct Functions


/* typically in header file */
typedef struct NAME_TAG{
    int DataMember;
} Name;
void Name_foo(Name *name, int param);

/* typically in	c file */
void Name_foo(Name *name, int param){
    name->DataMember += param;
}

/* typically in	another c file */
Name MyName = {4};
Name_foo(&MyName, 7);

C++ Struct Functions


// Typically in header file
struct Name{
    int DataMember;
    void foo(int param); // Member function
};

// Typically in cpp file
void Name::foo(int param){ 
    DataMember += param; // Python "self" is implied
    // could also do this->DataMember = param;
}

// Typically in	another cpp file
Name MyName = {4};
MyName.foo(7);

Static Members

C++ supports static members within structs, allowing shared data across all instances of the struct.

Static Member Example

C: Emulated through global variables.


/* Typically in header file */
extern int Name_DataMember;

/* Typically in c file */
int Name_DataMember = 4;

C++: True static members are supported.


// Typically in header file
struct Name{
    static int DataMember;
};

// Typically in cpp file
int Name::DataMember = 4;

Static Member Function Example

C: Emulated through normal functions.


/* Typically in header file */
extern int Name_DataMember;
void Name_foo(int param);

/* Typically in c file */
int Name_DataMember = 4;
void Name_foo(int param){
    Name_DataMember += param;
}

C++: Supports static member functions.


// Typically in header file
struct Name{
    static int DataMember;
    static void foo(int param);
};

// Typically in cpp file
int Name::DataMember = 4;

void Name::foo(int param){
    DataMember += param;
}

Exercises

  1. You can find the in-class programming exercises here.

Review Questions

  1. Explain the difference between pass-by-reference in C and C++. Provide a code example for each.
  2. Write a C++ function that uses default arguments. Explain how default arguments can simplify function calls.
  3. Create a struct in C++ with a static member. Write a function to modify this static member and demonstrate its usage.
  4. Discuss the advantages of using member functions within structs in C++ compared to C.