C++类与对象

4.3Circle类
#include<iostream>
using namespace std;
//宏常量
const float PI=3.141593;
const float FENCE_PRICE=35;
const float CONCRETE_PRICE=20;
//圆类
class Circle{
public:
	Circle(float r);//构造函数
	float circumference();//成员函数计算周长
	float area();//计算面积
private://私有成员
	float radius;
};
//类中成员函数的实现
Circle::Circle(float r){
	radius=r;
}
float Circle::circumference(){
	return 2*PI*radius;
}

float Circle::area(){
	return PI*radius*radius;
}

int main(){
	float radius;
	cout<<"Enter the radius of the pool:";
	cin>>radius;

	Circle pool(radius);//边界对象
	Circle poolRim(radius+3);//栅栏对象

	float fenceCost=poolRim.circumference()*FENCE_PRICE;
	cout<<"Fencing Cost is $"<<fenceCost<<endl;

	float concreteCost=(poolRim.area()-pool.area())*CONCRETE_PRICE;
	cout<<"Concerte COst is $"<<concreteCost<<endl;

	return 0;
}
//构建了一个圆类,初始化了两个对象,并且分别调用了各个对象的成员函数,用其私有变量计算出所需的值

4.4类的组合

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

class Point{
public:
    Point(int xx=0,int yy=0){
        x=xx;
        y=yy;

        cout<<"Calling the constructor of Point "<<endl;
    }
    Point(Point &p);
    int getX(){return x;}
    int getY(){return y;}
private:
    int x,y;
};

Point::Point(Point &p){
    x=p.x;
    y=p.y;
    cout<<"Calling the copy constructor of Point"<<endl;
}
//类的组合,由point类组合而成的line类
class Line{
public:
    Line(Point xp1,Point xp2);
    Line(Line &l);
    double getLen(){return len;}
private:
    Point p1,p2;
    double len;//线段的长度由构造函数计算得出
};

Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2){//p1=xp1,p2=xp2
    cout<<"Calling constructor of Line"<<endl;
    double x=static_cast<double>(p1.getX()-p2.getX());//static_cast<double>将后面的表达式强制转换成double类型
    double y=static_cast<double>(p1.getY()-p2.getY());
    len=sqrt(x*x+y*y);
}

Line::Line(Line &l):p1(l.p1),p2(l.p2){
    cout<<"Calling the copy constructor of Line"<<endl;
    len=l.len;
}

main(){
    Point myp1(1,1),myp2(4,5);
    Line line(myp1,myp2);
    Line line2(line);
    cout<<"The length of the line is :";
    cout<<line.getLen()<<endl;
    cout<<"The lenth of line2 is:";
    cout<<line2.getLen()<<endl;
}

4.7结构体

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

struct Student{
    int num;
    string name;
    char sex;
    int age;
};

main(){
    Student stu={97001,"Lin Lin",'F',19};
    cout<<"Num:  "<<stu.num<<endl;
    cout<<"Name: "<<stu.name<<endl;
    cout<<"Sex:  "<<stu.sex<<endl;
    cout<<"Age:  "<<stu.sex<<endl;
}
//结构体和类的区别:
//结构体是特殊的类,默认成员为公有类型,而类为私有
//C++引入结构体是为了保持和C语言的兼容性


4.8结构体作为类的成员变量

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

class ExamInfo{
public:
    //三种构造函数
    ExamInfo(string name,char grade):name(name),mode(GRADE),grade(grade){}
    ExamInfo(string name,bool pass):name(name),mode(PASS),pass(pass){}
    ExamInfo(string name,int percent):name(name),mode(PERCENTAGE),percent(percent){}
    void show();
private:
    string name;
    enum{
        GRADE,
        PASS,
        PERCENTAGE
    }mode;//记分方式
        union{
            char grade;
            bool pass;
            int percent;
        };

};

void ExamInfo::show(){
    cout<<name<<":";
    switch(mode){
        case GRADE:cout<<grade;
            break;
        case PASS:
            cout<<(pass?"PASS":"FALL");
            break;
        case PERCENTAGE:
            cout<<percent;
            break;
    }
    cout<<endl;
}

int main(){
    ExamInfo course1("English",'B');
    ExamInfo course2("Calculus",true);
    ExamInfo course3("C++Programming",85);//根据不同的数据类型调用不同的构造函数,自动产生不同的mode值
    course1.show();
    course2.show();
    course3.show();
    return 0;
}

5.3

#include<iostream>
using namespace std;
 class point {
 public:
     point (int x = 0, int y = 0):x(x),y(y){
        count++;
     }
     point (point &p){
        x = p.x;
        y = p.y;
        count++;
     }

     ~point(){count--;}
     int getX(){
        return x;
     }
     int getY(){return y;}
     void showCount(){cout<<count<<endl;}
 private:
    int x,y;
    static int count;
 };
 int point::count = 0;
 int main(){
    point a(4,5);
    cout<<a.getX()<<a.getY()<<endl;
    a.showCount();
    point b(a);
    b.showCount();
    return 0;
 }

#include<iostream>
using namespace std;
 class point {
 public:
     point (int x = 0, int y = 0):x(x),y(y){
        count++;
     }
     point (point &p){
        x = p.x;
        y = p.y;
        count++;
     }

     ~point(){count--;}
     int getX(){
        return x;
     }
     int getY(){return y;}
     static void showCount(){cout<<count<<endl;}
 private:
    int x,y;
    static int count;
 };
 int point::count = 0;
 int main(){
    point a(4,5);
    cout<<a.getX()<<a.getY()<<endl;
    point::showCount();
    point b(a);
    point::showCount();
    return 0;
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值