【记录】C++中的类成员调用

代码如下:

// 文件包含与预处理.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <windows.h>
#include <iostream>
using namespace std;

class fruit
{
public:
	fruit(const string nst,const string cst) :name(nst), colour(cst)
	{

	}
	void print()
	{
		cout << name << endl;
		cout << colour << endl;
	}
	void Ck(int i)
	{
		for (; i < 10; i++)
		{
			printf("%d", i);
		}
	}
private:
	string name;
	const string colour;
};


int _tmain(int argc, _TCHAR* argv[])
{
	fruit apple = fruit("a", "1");
	fruit orange = fruit("b", "2");
	apple.print();
	orange.print();
	fruit a=a;
	a.Ck(6);
	getchar();
	return 0;
}

  在类成员中的公共函数,这里我申明了一个Ck函数名

里面嵌套了for循环,当调用的时候类初始化对象调用了Ck

直接通过类名去调用是不行的,这与C不同的地方在于初始化了一个对象去调用。

fruit a=a; 这一块网上有很多种解释,最后看反汇编也没个苗头,反正能调用出来就行了。

留张图。:

原文地址:https://www.cnblogs.com/blck/p/8476636.html