C is not enough cuz:

  • We’d like to bundle data and functions together
    • Cux it’s organized
    • It represents “a unit of functionalit”
      • In C, CDTs can’t do anything by itself.
    • Information hiding
      • We can’t do it in C
      • It’s bad to not have info hiding cuz data ownership:
        • You can have CDts and things but the important thing is the head of a list
        • However the API doesn’t have control over the head; a user could change the head illegally
    • We want options. We want to support multiple versions of the same algorithm/function
      • There is no overloading in C, so there cannot simultaneously exist int AdjMat() and double AdjMat()

OOP time

  • Encapsulation
  • Classes bundle functionality together
  • Info Hiding
  • Control of data / functions / what is visible to the user
    • Public can be seen outside the class code
    • Private only visible inside the class code
    • The compiler is what enforces access modifiers. It’ll throw an error if you try to access things le wrong way. methods .fcns for classes data members fields

constructors: instead of “remembering” to initialize the data of a CDT via a function, we have the constructor method to automatically do that all the time.

The compiler cannot predict things. Like if you somehow find a pointer to a private member, then the compiler won’t catch that. Even at runtime, the compiler will not know :(

  • Therefore, there is not any immutable/mutable things in C OR C++
class Vault
{
	private:
		char safe_string[100]
	public:
		char not_safe[100];
}
 
int main()
{
	Vault vault = new Vault();
	// Unfortunately this thing works :( 
	strcpy(safe_string + 100, "I have changed a private thing!");
}

Implementations for functions of classes are of the form classname::functionname with everything else being a copy of the function we are implementing (the same signature).

The destructor clears things up before the object leaves scope.

Fun trivia:

  • Instead of storing the code for a method in a class, functions of classes are really just pointers, that points to the function in memory and does the right thing.

I NEEDA TRY THIS:

  • If you have the memory position of a function in C++, you can swap out the function with your own evil function AND EVERY CALL OF THE CLASS FUNCTION WILL CALL THE EVIL ONE!?todo

Function Overloading

You can have:

  • int graph_DFS(AdjMat, i, j, d)
  • int graph_DFS(AdjList, i, j, d)
  • void graph_DFS(AfjList, i, [list of j])
  • It’s possible to have a function of the same name, but different signatures and implementations
  • the compiler figures out which one to call depending on the args.
  • This is called polymorphism
    • Translates to “many forms”
    • Same thing but many shapes
    • One way to do polymorphism is by overloading functions
    • Another way is with data:
      • You might have 4 CDT’s for books, periodicals, CDs, and DVDs
      • You don’t want to have many copies of data, though. So we make a super CDT or something lol and make the 4 CDTs inherent from it
  • Idea: Develop a hierarchy if related / similar items.
    • I.e. we create some generic item
  • and that will be our parent class; that is a blueprint for child classes. This will be info or functionality that is common between the 4 CDTs.
  • You CANNOT have two functions with identical signatures but different return types (the compiler will have no way of knowing which one to use) ^ This is false. Paco you cannot be spewing lies and misinformation like this!

C++ syntax things

  • new allocates memory for a class
  • delete deletes it
  • No need to use calloc or free