#include <iostream>
class Entity{
private:
float x,y;
public:
Entity(){
x=0;
y=0;
}//constructor is just like a method used for initiating members when you instantiate an instance
//Constructor with parameters
Entity(float a, float b){
this->x=a;
this->y=b;
}
void print(){
std::cout<<x<<","<<y<<std::endl;
}
};
class Log{
//if you don't want the class to be instantiated (only let others use the static methods
private:
// Log(){}
public:
//there is a default constructor for class
//Log(){do nothing}
// Log()=delete;
static void Function(){}
};
int main(){
Entity test1;
test1.print();
Entity test2(11.3,22.3);
test2.print();
Log l;//you will receive an error message and you can't instantiate an instance
//在创建类实例时会调用构造函数 如果构造函数设为private 类外无法调用 则无法创建实例
Log::Function();
}
C++学习笔记|Constructor|Cherno C++ Tutorials
最新推荐文章于 2022-09-07 17:41:30 发布