zoukankan      html  css  js  c++  java
  • 【记录】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; 这一块网上有很多种解释,最后看反汇编也没个苗头,反正能调用出来就行了。

    留张图。:

  • 相关阅读:
    传参总结
    装mongondb数据库
    es6的几种写法
    在github上搭建自己的主页
    云主机,虚拟主机,VPS
    TDD vs. BDD
    流量统计分析软件比较
    深入浅出Node js 读书笔记 思维导图
    浏览器的工作原理解析
    开源搜索引擎的比较
  • 原文地址:https://www.cnblogs.com/blck/p/8476636.html
Copyright © 2011-2022 走看看