码迷,mamicode.com
首页 > 编程语言 > 详细

面向对象学习【c++】

时间:2015-06-10 08:55:57      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:c++   代码   

c++太弱了(其实是一点都不会!)
挖个坑来学习c++!
不间断更新!

代码1:

#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

using namespace std;
/*
    类编译时 首先编译成员 再编译函数体
    所以 成员函数可以直接使用类中的其他成员 无须在意出现的次序
    成员函数在类内部声明  内部或外部定义
*/
class Sale_data 
{
public :
    Sale_data() = default;//默认构造函数
    Sale_data(const string &s ,double p) ://构造函数
        bookNo(s) ,revenue(p) { }

    double avg_price() const;
    string isbn() const
    {
        return this->bookNo;//this 为 常量指针
    }
    Sale_data& combine(const Sale_data&);

    friend bool comare(Sale_data &is)//友元函数  可访问私有成员
    {
        return is.is_ok;
    }
    string bookNo = "you are a pig";
    unsigned units_sold;
    double revenue;

private:
    bool is_ok;

};

double Sale_data::avg_price() const//在外部定义函数体
{
    if (units_sold)
        return revenue / units_sold;
    else
        return 0;
}

Sale_data& Sale_data ::combine(const Sale_data &rhs)
{
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;//返回调用该函数的对象
}

////////////声明函数

Sale_data add(Sale_data&,const Sale_data&);
std::ostream &print(std::ostream&, const Sale_data&);
std::istream &read(std::istream&, Sale_data&);

////////////定义 read print 函数

istream &read(istream& is, Sale_data& item)
{
    double price = 0;
    is >> item.bookNo >> item.units_sold >> price;
    item.revenue = price * item.units_sold;
    return is;
}

ostream &print(std::ostream& os, const Sale_data& item)
{
    os << item.isbn() << " " << item.units_sold << " "
        << item.revenue << " " << item.avg_price() << endl;
    return os;
}
////////////定义 add 函数

Sale_data add(Sale_data& rhs, const Sale_data& lhs)
{
    Sale_data sum = lhs;// 存放和
    sum.combine(rhs);
    return sum;
}

//////////// 类外部定义构造函数
/*
Sale_data::Sale_data(std::istream &is)
{
    read (is, *this);//read 函数从 is 读取数据存入this对象中
}
*/

////////////
Sale_data total;
Sale_data trans;


/////////// 主函数
int main()
{
    cout << total.isbn() << endl;
    total.combine(trans);

    ///////// 类的拷贝
    total = trans;
    //等价于
    total.bookNo = trans.bookNo; //bookNo等成员。。。

    return 0;
}

代码2:

#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

using namespace std;

class Screen
{
public:
    typedef string::size_type pos;//定义类型的成员必须先定义 后使用
    //using pos = string::size_type;
    Screen() = default;
    Screen(pos ht) :hight(ht) { }
    string get() const
    {
        return contents;
    }
    Screen &move(pos r, pos c);
    Screen &set(char);
private:
    pos cursor = 0;
    pos hight = 0, width = 0;
    string contents;
};

inline Screen&  Screen::move(pos r,pos c) //inline用于定义内联函数
{
    pos row = hight * width;
    cursor = hight + 1;
    return *this;
}
//函数重载: 函数名形同 参数个数 类型 不同的函数

inline Screen &Screen::set(char c)
{
    contents[cursor] = c;
    return *this;
}

Screen myscreen;

int main()
{
    myscreen.set(‘#‘);
    return 0;
}

代码3:

class Screen
{
public:
    friend class Window_mgr;// 友元类 友元关系不传递
    friend void Window_mgr::clear(); // 其他类的函数的友元
private:

};

class  Window_mgr
{
public:
    void  clear();
private:

};

Screen myscreen;

int main()
{
    return 0;
}

面向对象学习【c++】

标签:c++   代码   

原文地址:http://blog.csdn.net/u014427196/article/details/46434541

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!