void printData(double x) { cout << "Print double: " << x << endl;}void printData(float x) { cout << "Print float: " << x << endl;}int main() { char a = '0'; printData(a); // Automatic type conversion return 0;}
Class and objects: basic concepts and syntax
#include <iostream>using namespace std;class Circle {public: float x, y, r; void setCenter() { cout << "Input center:" << endl; cin >> x >> y; } void setRadius() { cout << "Input radius:" << endl; cin >> r; } // can define it later bool isWithin(float x0, float y0); float perimeter(); float area();};int main() { Circle a; a.setCenter(); a.setRadius(); cout << "The perimeter of circle a is " << a.perimeter() << endl; Circle *b = new Circle(); b->setCenter(); b->setRadius(); cout << "The area of circle b is " << b->area() << endl; delete b; return 0;}
this Pointer
class Circle {public: float x, y, r; void setCenter(float x, float y) { this->x = x; this->y = y; } void setRadius(float r) { this->r = r; }};
Constructors and destructors
#include <iostream>using namespace std;class Circle {public: float x, y, r; Circle() { cout << "Input center and radius:\\n"; cin >> x >> y >> r; } Circle(float x0, float y0, float r0) { x = x0; y = y0; r = r0; }};int main() { Circle *a = new Circle(); delete a; Circle b(0, 0, 1); Circle c; // Circle() will be called c = Circle(1, 1, 2); // assign new object return 0;}
Default Constructor
#include <iostream>using namespace std;class Circle {public: float x, y, r;};int main() { // if no constructor is defined, default constructor is added Circle a; return 0;}
#include <iostream>using namespace std;class Circle {public: float x, y, r; // if constructor is defined, no default constructor is given Circle(float x0, float y0, float r0) { x = x0; y = y0; r = r0; }};int main() { Circle a; // illegal Circle *b = new Circle() // illegal return 0;}
Initializer List
class Circle {public: float x, y, r; Circle(int x, int y, int r):x(x), y(y), r(r) {} // while is equivalent to // Circle(int x0, int y0, int r0) { // x = x0; y = y0; r = r0; // }};
class myClass {public: // const and reference variable MUST be initialized const int t1; int& t2; // Initializer list must be used myClass(int t1, int& t2):t1(t1), t2(t2) {}};
Destructor
#include <iostream>using namespace std;class Robot {public: char *name = NULL; Robot(char *name) { int n = strlen(name); this->name = new char[n + 1]; strncpy(this->name, name, n); this->name[n] = '\\0'; cout << "Constructing " << name << endl; } ~Robot() { cout << "Destructing " << name << endl; // it's a good practice to free memories allocated // for member variables in destructor delete name; }};void func() { Robot eve("Eve"); cout << "func is about to return\\n"; // Automatically calls the destructor when a // statically allocated object is out of the // scope}int main() { Robot *wall_e = new Robot("Wall-e"); func(); // A dynamically allocated object is destructed // only when you explicitly delete it delete wall_e; cout << "main is about to return\\n"; return 0;}