本测试代码包括以下内容:

(1)如何使用构造函数;
(2)默认构造函数;
(3)对象间赋值;
(4)const使用语法;
(5)定义类常量: 一种方法是用enum,另一种方法是使用static。

#include <iostream>

using namespace std;

enum sexType
{
MAN,
WOMAN
}; class Human
{
//the default is private
private:
string name;
sexType sex;
int age; //(5) 定义类常量: 一种方法是用enum,另一种方法是使用static
enum{LEN=};
static const int LEN2 = ; public:
//如果类定义中没有提供任何构造函数,则编译器提供默认构造函数。但,如果类中定义了构造函数,那么编写者必须同时提供一个默认构造函数。
//有两种方法提供默认构造函数:
//(1) 定义一个没有参数的构造函数:Human();
//(2) 为非默认构造函数的参数提供默认值: Human(string m_name="no name", int m_age=0, sexType m_sex=MAN);
//两种定义方式只能二选一
Human();
Human(string m_name, int m_age, sexType m_sex);
Human(int m_age);
~Human(); //定义在类声明中的方法为内联方法。也可以使用inline关键字将函数定义在类声明外部。
void show() const //const加在函数名后面表示该函数不会修改该类的数据成员。
{
cout<<"This is "<<name<<", sex: "<<sex<<", "<<age<<" Years old."<<endl;
} }; Human::Human()
{
cout<<"default construct function"<<endl;
} Human::Human(string m_name, int m_age, sexType m_sex)
{
cout<<"construct function: "<<m_name<<endl;
name = m_name;
age = m_age;
sex = m_sex;
} Human::Human(int m_age)
{
age = m_age;
} Human::~Human()
{
cout<<"destroy function: "<<name<<endl;
} int main()
{
cout << "This is test code of C++ class: "<< endl;
{
//(1) use of construct function
Human jack = Human("Jack", , MAN); //显示调用
Human jerry("Jerry", , MAN); //隐式调用
Human *pTom = new Human("Tom", , MAN); //New调用
//当构造函数只有一个参数时,可以直接用赋值语句赋值。只有一个参数的构造函数将会被自动调用
Human marry = ; //赋值调用 //(2) defaults construct function
Human Lucy; //(3) 赋值对象
Human James;
James = Human("James", , MAN); //创建一个临时对象James,copy一份儿该对象赋值给James变量。紧接着该临时对象会被销毁。 //(4) const
const Human Thomas("Thomas", , MAN);
Thomas.show(); //The show method must define with 'const'
}
return ;
}

运行结果为:

[C++]C++类基本语法的更多相关文章

  1. go类c语法

    go类c语法 一般来说,如果一门语言具有类c语法,意味着当你习惯使用其他类c语言例如c.c++.java.javascript和c#,然后你就会发现go语言和它们也类似,至少表面上是.例如,使用&am ...

  2. ES2015 类 class 语法

    在ES2015之前,定义类的方法只能通过原型链来模拟 function Animal(family,species) { this.family = family; this.species = sp ...

  3. python类的语法和底层实现

    语法: class 类名: name = “egon”    # 类属性 def __init__(self): self.age = 18  # 对象属性 self.__sex = "fe ...

  4. C# 面向对象2 (类的语法)

    1.类 语法: [public] class 类名 { 字段; 属性; 方法; } **类名首字母必须大写 2.创建对象 创建这个类的对象过程称之为类的实例化,关键字:new this:表示当前这个类 ...

  5. 什么是静态内部(Static Inner)类,语法要注意什么?

    4静态内部类(Static Inner Classes) 马克-to-win:这里的内部类的static,意思是它可以不用实例化外部类,就自己单独被实例化,单独存在(有点像生活中的办公室和办公桌(独立 ...

  6. 类模板语法知识体系梳理(包含大量常犯错误demo,尤其滥用友元函数的错误)

    demo 1 #include <iostream> #include <cstdio> using namespace std; //template <typenam ...

  7. 【c++错误】类的语法错误 error c2533:constructors not allowed a return type(构造函数不允许返回一个类型)

    今天编写类的程序的时候不小心把类后的分号忘写了,就出现上面的错误提示. 顺便复习下类的正确格式: class 类名 { public: //习惯上将公有类型放在前面,便于阅读 ……(外部接口) pro ...

  8. python 类高级语法 静态方法

    通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方 ...

  9. 【yml】springboot 配置类 yml语法

    参考:https://www.runoob.com/w3cnote/yaml-intro.html YAML 是 "YAML Ain't a Markup Language"(YA ...

随机推荐

  1. 【ajax】reqwest

    ded / reqwest All over again. Includes support for xmlHttpRequest, JSONP, CORS, and CommonJS Promise ...

  2. HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

    Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  3. iOS开发:使用Tab Bar切换视图

    iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...

  4. HDOJ 2212 DFS

    Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...

  5. 装饰模式,制作一个蛋糕java

    import java.text.DecimalFormat; //抽象组件组件 interface mkcake { public void cake(); } class Cake impleme ...

  6. Ado Recordset.open

    Recordset.open Recordset 对象的 Open 方法 允许用户向数据库发出请求,通过是运行一个 SQL命令.启动一个指定的数据表或调用一个指定的 Stored Procedure ...

  7. sqlplus乱码

    使用SecureCRT或是pietty_ch连接到一台安装有Oracle DB 10g的RHEL4.2的机器,linux使用的shell是默认的bash. 在bash提示符下,使用Del键或者Back ...

  8. November 4th Week 45th Friday 2016

    Problems are not stop signs, they are guidelines. 问题不是休止符,而是指向标. Most of the problems can be overcom ...

  9. Python自带的日志模块logging的使用

    import logging     # 创建一个logger logger = logging.getLogger('cmccLogger') logger.setLevel(logging.DEB ...

  10. 分布式还是混合式? 谈CDN架构对服务质量的影响

    传统分布式模型 通 常,内容分发网络(CDN)採用分布式模型.在这样的模型里, 用户的文件存放在一个源server上.而且由大量边缘server负责分发这些文件.这些边缘server的磁盘空间比較小. ...