#include <iostream>
class Pair {
public:
int *pa,*pb;
Pair(int, int);
Pair(const Pair &);
~Pair();
};
// Custom Ctor
Pair::Pair(int a, int b) {
pa = new int(a);
pb = new int(b);
}
//Custom Copy Ctor
Pair::Pair(const Pair & obj) {
//std::cout << *(obj.pa) << std::endl;
pa = new int(*(obj.pa));
pb = new int(*(obj.pb));
}
// Custom Destructor
Pair::~Pair() {
delete pa;
delete pb;
}
int main() {
Pair p(15,16);
Pair q(p);
Pair *hp = new Pair(23,42);
delete hp;
std::cout << "If this message is printed,"
<< " at least the program hasn't crashed yet!\n"
<< "But you may want to print other diagnostic messages too." << std::endl;
return 0;
}
#pragma once
namespace basic {
class cube {
public:
/* EVERY OBJECT NEEDS TO BE BUILT FIRST BY A CONSTRUCTOR, THEN LATER YOU CAN ASSIGN AND CANNOT RECONSTRUCT IT*/
/*
Class constructor:
When an instance of a class is created in C++, the class constructor lets us program
exactly the initial state of the object that's created.
Automatic => if you don't specify: random value
Custom default, name of function is the same with the name of the class, can be called without bracket
Custom Single/Double/etc arguments, specified; called with arguments
*/
cube(); //custom default ctor
cube(double len); //custome single params
/*
Copy Constructor:
a copy ctor is a special type of constructor that allows us to make a copy of an existing object.
Copy the contents of all member variables
> Automatic
> Custom Copy ctor: has exactly one argument of the same type of the class and passing an object
*/
cube(const cube & obj);
/*
Assignment Operator:
Because after the obj is constructed you cannot reconstruct it, so the only way to change the value is to assign a new value
It can be done automatically, but also if you want to specify multiple obj or allocate a resource, then you need to define
a custom asssignment operator:
1. Should be public member
2. function name is operator=
3. Has return value of a reference of the class' type (.....&)
4. Has exactly one arguments of const reference of the class type
*/
cube & operator=(const cube & obj);
/*
Destructor:
At the end of the class lifecycle to cleanup, either the defined custom destructor,
or invoke by delete if the class is in the heap.
1. No need return
2. Use tilde ~
*/
~cube();
double getVol() const; //Not alter later
double getTotArea() const;
void setLen(double len);
private:
double len_;
};
}
#include <iostream>
#include "cube.h"
namespace basic {
// Custom Default Ctor
cube::cube() {
len_ = 5;
std::cout << "Custom Default Ctor applied" << std::endl;
}
// Custom Single Argument Ctor
cube::cube(double len) {
len_ = len;
std::cout << "Created $" << getVol() << std::endl;
}
// Custom Copy Ctor
cube::cube(const cube & obj) { //ampersand & is a reference and it is used for passing object as argument
len_ = obj.len_;
//std::cout << "Custom Copy Ctor applied" << std::endl;
std::cout << "Created $" << getVol() << " via copy" << std::endl;
}
// Custom Assignment Operator
cube & cube::operator=(const cube & obj) {
len_ = obj.len_;
//std::cout << "Custom Assignment applied" << std::endl;
std::cout << "Transformed $" << getVol() << " -> $" << obj.getVol() << std::endl;
return *this; //Dereference value of instance (object) of the class itself; this technique can also be used for method chaining
}
// Custom Destructor
cube::~cube() {
std::cout << "Destroyed $" << getVol() << std::endl;
}
double cube::getVol() const{
return len_*len_*len_;
}
double cube::getTotArea() const{
return 6*len_*len_;
}
void cube::setLen(double len) {
len_ = len;
}
}
#include <iostream>
#include "cube.h"
#include "triangle.h"
//Use this for easy code
using namespace std;
using namespace basic;
void foo(cube cube) {
//Do nothing; TO show passing an object will invoke copy ctor
}
cube bar() {
cube newLocalCube;
return newLocalCube;
}
double cube_on_heap() {
cube * c = new cube(5);
cube * b = new cube();
delete c;
c = nullptr; //Always assign null pointer after delete
return b->getVol();
}
double cube_on_stack() {
cube a(8);
return a.getVol();
}
template <typename T>
T maks(T a, T b) {
if(a > b) {return a;}
return b;
}
int main() {
// CREATE NEW OBJECT
//cube newCube; //default 5 with custom default ctor
cube Cube1(10); //with custom single param ctor
/*
Custom copy ctor will active given the following condition:
1. Passing an object as a param (by value)
2. Returning an object from a function (by value)
3. Init a new object
*/
// 1. Passing an object
//foo(newCube); //Custom Copy Ctor
// 2. Returning an object from a function
//cube copiedCube = bar();
// 3. Init a new object
//cube initNewCube = newCube;
//Invoke Copy Ctor
//cube alice_wallet = newCube;
// Invoke assignment operator
//cube bruce_wallet;
//bruce_wallet = newCube;
/*
Storage by reference (&):
> Zero size variable, only an alias, not real in the memory
> When init, you need to alias something that already been created
*/
//Create reference as alias so no need to create new obj, but transfer the ownership
cube & cindy_wallet = Cube1;
//By pointer
cube Cube2(5);
cube * daniel_wallet = &Cube2;
//Default Constructed Obj
cout << "Volume: " << Cube1.getVol() << endl;
cube_on_heap();
cube_on_stack();
// double len = 0.0;
// cin >> len;
// Cube1.setLen(len);
// cout << "Volume: " << Cube1.getVol() << endl;
/* Templated Function Test*/
cout << "max(5,3) = " << maks(5,3) << endl;
cout << "max('a', 'z') = " << maks('a', 'z') << endl;
/* Inheritance Test */
// !!! Always update the make file after you add new file
triangle segitiga(2, 5);
cout << "Triangle Area derived from shape: " << segitiga.getArea() << endl;
return 0;
}
//Easy compile: g++ -o cube main.cpp cube.cpp
#pragma once
#include "shape.h"
namespace basic {
class triangle : public Shape {
public:
triangle(double height, double width);
double getArea() const;
private:
double height_;
};
}
#include "shape.h"
#include "triangle.h"
namespace basic {
triangle::triangle(double width, double height) : Shape(width) {
height_ = height;
}
/**
* Returns area of a triangle.
*
* This is how you create functions description.
*
* @param -
*
* @return The area of triangle in floating point precision.
**/
double triangle::getArea() const {
return getWidth() * height_ / 2.0;
}
}
#pragma once
class Shape {
public:
Shape();
Shape(double width);
double getWidth() const;
private:
double width_;
};
#include "shape.h"
Shape::Shape() : Shape(1) {
//Default
}
Shape::Shape(double width) : width_(width) {
//Custom
}
double Shape::getWidth() const {
return width_;
}
In this notespace, I will share my original journey of thinkering with IoT. The mission is to create a monitoring system to gather data from a drying device and save it into a datasheet.
Mar 29, 2025Programmer
Mar 17, 2025Strategy is a plan of change to achieve an objective
Sep 15, 2024Basic control requirements:
Jul 31, 2024or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up