1. 程式人生 > >兩個C++cc程序

兩個C++cc程序

日常

#include <iostream>
#include <string>
using namespace std;

class Book
{
public:
Book(string na, string au, int page, float price):
name(na),author(au),page(page),price(price)
{
cout<<"書名:"<<name<<endl
<<"作者:"<<author<<endl
<<"頁數:"<<page<<endl

<<"價格:"<<price<<endl;
}

Book(string na, string au):
    name(na),author(au)
{
    cout<<"書名:"<<name<<endl
        <<"作者:"<<author<<endl;
}

Book()
{
    cout<<"新建Book對象"<<endl;
}

Book(const Book &b)

{
name = b.name;
author = b.author;

page = b.page;
price = b.price;
cout<<"書名:"<<name<<endl
<<"作者:"<<author<<endl
<<"頁數:"<<page<<endl
<<"價格:"<<price<<endl;
}

~Book()
{
    cout<<"回收Book對象"<<endl;
}

private:
string name;

string author;
int page;
float price;
};

int main()
{
Book b1("sa","zero",12,34);
Book b2("sa","zero");
Book b3;
Book b4 = b1;
return 0;
}技術分享圖片
#include<iostream>
#include<math.h>
using namespace std;
class Point
{
public:
Point(int a,int b):x(a),y(b){}
void display();
friend float Sum(Point &,Point &);
private:
int x;
int y;
};

void Point::display()
{
cout<<x<<","<<y<<endl;
}
float Sum(Point &m,Point &n)
{
float su;
su = sqrt((m.x-n.x)(m.x-n.x)+(m.y-n.y)(m.y-n.y));
cout<<su<<endl;
}

int main()
{
Point b1(4,6);技術分享圖片
Point b2(2,3);
b1.display();
b2.display();
Sum(b1,b2);
return 0;
}

兩個C++cc程序