Notes for C++ (1)
Files and Classes
Header
Header files are used like interfaces in Java:
Square.h
#pragma once
// include guard
// to stop circular imports of headers from looping non-stop
namespace test123 {
class Square {
public:
Square(); // constructor with no param; default constructor
Square(double length); // custom constructor
// Square(const Square &s); //a copy constructor
// will use default copy constructor here
double getArea(); // member method
// double getArea() const;
// const means it is not going to change the instance
private:
double length_;
}
}
When constructor is not written, c++ will automatically run the default constructor, which takes no parameter, allocates the spaces (for variables) but not initializing any standard typed variables.
CPP files
cpp (C++) files are for detailed implementations and codes.
Square.cpp
#include "Square.h"
// "" means search the current directory
// <> means search the system (e.g. standard library), as well as directory
namespace test123 {
Square::Square(){
// :: scope resolution operator
// if 'namespace test123' is not written
// then it should be test123::Square::Square()
// namespace->class->method
length_ = 1.0;
}
Square::Square(double length) {
length_ = length;
}
double Square::getArea() {
return length_ * length_;
}
}
Test.cpp
#include "Square.h"
// importing the interface
using test123::Square;
// shortcut test123::Square to global
#include <iostream>
// importing standard lib for io
using std::cout;
using std::endl;
// shortcut cout and endline into global
// endl is a string for ending a line, depending on the system (\n or \r\n)
int main() {
Square s1(2); // init with param (length:2)
Square s2; // init with no param
cout << "Area of square 1: " << s1.getArea() << endl;
cout << "Area of square 2: " << s2.getArea() << endl;
return 0; // return 0 for successful run and exit
}
Other note:
Struct vs Class: They are mostly the same in C++, except that by default, struct is all public and class is all private.
Inheritance
// Shape.h
class Shape {
public:
Shape();
Shape(double length);
double getLength() const;
private:
double length_;
};
// header file
#pragma once
#include "Shape.h"
class Square : public Shape { //inheritance
public:
double getArea() const;
private:
//nothing
};
Note:
- Public members of the base class get copied into the derived.
- Private members of the base class remains in base. (not accessible by derived class directly)
Polymorphism
The idea that a single interface may take multiple types, or a single symbol may be different types.
- Interface that takes different parameters
- Derivative class instances in Base variable ( Shape shape = new Square(1);)
Keyword virtual
virtual Shape::print1(){
// virtual means: if the real instance is a derivative class instance
// and if that derivative class implemented print1()
// then use derivative class's print1 function
cout << "Shape" << endl;
}
//if in .h file
virtual Shape::print2() = 0; // this is a pure virtual function
// Base class with any pure virtual function is a abstract base class
// Abstract base class must have derivative class to have instances
- If performance is really really important, avoid using keyword virtual
- Destructors always need to be virtual, so they can destruct the real class and release memory
Memory
Pointers and references
Square s1; // instance of a Square object
// it allocates space for the object
Square cp = s1; // make a new instance which is a copy of s1
// copy constructure
Square &r1 = s1; // reference to a Square instance
// when creating a reference, it must be initialized
// r1 and s1 are the same variable. They point to the same memory
Square *p1; // pointer to a Square object
p1 = & s1; // p1 is now pointing to address of s1
Indirect operators:
&variable; // address of the variable
*pointer; // dereference - get the data from the pointer address
v->getArea(); // same as (*v).getArea() - dereference, then get member
ref: https://courses.engr.illinois.edu/cs225/fa2020/