C++(例题集—简单解析-类与对象)

由于自己本人上课不太认真,只能靠自己学习例题总结。

希望能给大家一些便捷,让大家开心学习C++。

对象的创建与使用:

例题5-1    定义一个点类,生成对象,将点的坐标显示在屏幕上:

#include
using namespace std;
class Point{
    int x,y;
public:
    void setXY(int a,int b){
        x=a;
        y=b;
    }
    int getX(){
        return x;
    }int getY(){
        return y;
    }
};
int main()
{
    Point p1,p2;
    p1.setXY(1,2);
    p2.setXY(3,4);
    cout<<"点p1的x坐标为:"<

例题5-2 观察类的所占的字节:

#include
using namespace std;
class A{
    int x;
public:
    void setX(int b){
        x=b;
    }
};
int main(){
    A a;
    cout<

类成员的访问能力

例题5-3:观察类成员访问控制能力:

#include
using namespace std;
class A{
private:
    int i;
public:
    char c;
public:
    void setI(int j){
        i=j;
    }
    void display(){
        cout<<"i="<#include
#include
using namespace std;
const double Pi=acos(-1);
class Circle{
private:
    double m_r;
public:
    void setR(double r){
        m_r=r;
    }
    double getR(){
        return m_r;
    }double getArea(){
        return Pi*m_r*m_r;
    }
};
int main()
{
      Circle c;
      c.setR(3);
      cout<<"Circle's r is "<

通过这个实例其实可以看出来:

调用公有的成员函数或者变量:直接用    “.”来实现好比C语言里的结构体。


实例2:设计一个数字时钟,可以设置时间和显示时间

#include
using namespace std;
class Clock{
private:
    int h;
    int m;
    int sec;

public:
    void setTime(int nh,int nm,int ns){
        h=nh;
        m=nm;
        sec=ns;
        if(h<0||h>23||m<0||m>59||sec<0||sec>59){
            cout<<"illegal! time"<

类的信息隐藏

由于为了实现利用类的接口,需要把设置        .h    和    .cpp    两个文件来分别实现。

.h     负责实现提供类的声明和类函数的实现,

.cpp    负责利用类对应的接口函数来实现对应功能即可.


建立2个文件,分别存放长方形Rectangle 类的声明和实现、类的使用。

//#ifndef RECTANGLE_H_INCLUDED
//#define RECTANGLE_H_INCLUDED
class Rectangle{
    int len,w;
public:
    void setL(int len);
    void setW(int w);
    int getL();
    int getW();
    int getArea();
};


//#endif // RECTANGLE_H_INCLUDED
#include"Rectangle.h"
#include
using namespace std;
void Rectangle::setL(int L){
    len=L;
    if(L<0){
        printf("L:illegal!\n");
        exit(1);
    }
}
void Rectangle::setW(int W){
    w=W;
    if(W<0){
        printf("W:illegal!\n");
        exit(1);
    }
}
int  Rectangle::getL(){
    return len;
}
int  Rectangle::getW(){
    return w;
}
int Rectangle::getArea(){
    return len*w;
}
int main(){
    Rectangle A;
    A.setL(10);
    A.setW(20);
    cout<<"L:    "<

对象的初始化与消亡:

构造函数

由于类的复杂性,有必要专门设计一个函数来进行对象的初始化工作,

引例:初始化时刻需要做点是什么.如定义了一个新的对象时,需要设计一些提示词:

如“创建完成,目前是第N个对象”.

一个简单的例子:

#include
using namespace std;
class A{
    int i;
public:
    A(){
        i=0;
        cout<<"无参析构函数被调用"<<"i="<#include
using namespace std;
class A{
    int i;
public:
    A(int k=0){
        i=k;
        cout<<"带参构造函数调用"<<"i="<

实例:建立一个类Person(人),在屏幕上显示出基本信息:

#include
#include
using namespace std;
class Person{
    char *namePtr;
    bool sex;
    int age;
public:
    Person(char *name,bool sex,int age);
    ~Person();
    void display();
};
Person::Person(char *p,bool s,int a){
    namePtr = new char [strlen(p)+1];
    strcpy(namePtr,p);
    sex=s;
    age=a;
}
Person::~Person(){
    delete []namePtr;
}
void Person::display(){
    if(sex){
        cout<

对象的赋值与复制

例子、对象的赋值:

#include
using namespace std;
class A{
    int i;
    char c;
public:
    A(int j=0,char d='*'){
        i=j;c=d;
    }
    void display(){
        cout<<"i="<

对象的复制

复制构造函数

#include
using namespace std;
class A{
    int i;
    char c;
public:
    A(int j=0,char d='*'){
        i=j;c=d;
    }
    void display(){
        cout<<"i="<

例题5-9:浅复制示例。

某工厂生产了一批产品,所有产品质量属性默认为Good,若检测到某件产品质量有问题,应将质量属性改为Poor。

#include
#include
#include
using namespace std;
class Product{
    char * ptr;
public:
    Product (char * q){
        ptr=new char [strlen(q)+1];
        strcpy(ptr,q);
        cout<<"调用构造函数!!!"<

其实大家也可以看到:要是浅复制其实就是利用指针指向同一份数据,要是销毁一份数据,那么其他数据进行销毁

直接跳出系统。

例题5-10 深复制

深复制的要领:

开辟一个指针区域,把每一块对应开辟New,对应复制内容即可。

#include
#include
using namespace std;
class Product{
    char *ptr;
public:
    Product (char *q){
        ptr=new char [strlen(q)+10];
        strcpy(ptr,q);
    }
    Product(const Product &q){
        ptr=new char [strlen(q.ptr)+10];
        strcpy(ptr,q.ptr);
    }
    ~Product(){
        delete []ptr;
    }
    void change(char *q){
        strcpy(ptr,q);
    }
    void display(){
        cout<

复制函数被调用的三种情况

(1)、当用一个对象去初始化该类的另一个对象是。

(2)、当调用的函数形参是类的对象,在实参传值给形参是。

(3)、当调用的函数返回值是类的对象,在执行return语句向调用折传送对象时。

例题如下:

#include
using namespace std;
class A{
    int i;
public:
    A(int k=0){
        i=k;
        cout<<"调用了构造函数!!!!!!11"<

实例:圆类Circle,设计一个比较两个圆的大小.

#include
using namespace std;
class Circle{
    double R;
public:
    Circle(double r=1.0){
        R=r;
    }
    double getR(){
        return R;
    }
    bool isLargerThan(Circle q){
        return (R-q.R)>0;
    }
};
int main()
{
    Circle c1(2.0),c2(8.0);
    if(c2.isLargerThan(c1)){
        cout<<"c1c2"<

对象数组

定义:

#include
using namespace std;
class A{
    int i;
public:
    A(int k=0){
        i=k;
    }
    void show(){
        cout<<"i="<

例题:设计一个员工类(Employee),可以对所有员工加薪,并找出最高薪金员工。

#include
#include
using namespace std;
class Employee{
    char ID[10];
    char  *ptr;
    double s;
public:
    Employee();
    Employee(char id[],char *q,double ss);
    char *getName();
    double getS();
    void growS(double add);
    void display();
    ~Employee();
};
Employee::Employee(){
    ptr=NULL;
}
Employee::Employee(char id[],char *q,double ss){
    strcpy(ID,id);
    ptr=new char [strlen(q)+1];
    strcpy(ptr,q);
    s=ss;
}
Employee::~Employee(){
    delete []ptr;
}
char *Employee::getName(){
    return ptr;
}
double Employee::getS(){
    return s;
}
void Employee::growS(double d){
    s+=d;
}
void Employee::display(){
    cout<maxz){
            cnt=i;
            maxz=temp;
        }
    }
    cout<<"最高工资的人员是:"<

指向对象的指针与对象的引用

定义:

#include
using namespace std;
class A{
    int i;
public:
    A(int k=0){
        i=k;
    }
    void display(){
        cout<<"i="<display();
    p=&a2;
    p->display();
    return 0;
}

例题:对于在函数赋值

(1)、形参为对象。

(2)、形参为对象的引用。

(3)、形参为对象指针。

其中第一种是行不通的。

#include
using namespace std;
class A{
    int i;
public:
    A(){
        i=0;
        cout<<"调用了构造函数!!!\n";
    }
    A(const A & q){
        i=q.i;
        cout<<"调用了复制构造函数"<

this指针

例题5-14:设计长方形类,显式使用this指针
#include
using namespace std;
class R{
    int len,w;
public:
    R(int len,int w);
    int getArea();
};
R::R(int len,int w){
    this->len=len;
    this->w=w;
}
int R::getArea(){
    return (this->len)*(this->w);
}
int main(){
    R r1(2,5),r2(3,6);
    cout<<"First Area is "<


对象的动态创建与销毁:

主要运用了两个形式:

New:

new     ClassName(如:int);

有两种形式:

1、new     A(8)

2、new     A[3];

Delete:

1、delete     A;

2、delete     []A;

例题:

#include
using namespace std;
class A{
    int i;
public:
    A(int k=0){
        i=k;
    }
    void display(){
        cout<<"i="<display();
    delete p;
    p=new A(8);
    p->display();
    delete p;
    p=new A[3];
    A *q=p;
    for(int i=0;i<3;i++){
        q++->display();
    }
    delete []p;
    return 0;
}

例题5-15     设计点类,由用户决定点的数目及位置,将点展示在屏幕。

#include
using namespace std;
class Point{
    int x,y;
public:
    Point(int a=0,int b=0){
        x=a,y=b;
    }
    void setXY(int u,int v){
        x=u,y=v;
    }
    void display(){
        cout<<"("<>num;
    q=p=new Point[num];
    for(int i=0;i>x;
        cout<<"Enter y:";
        cin>>y;
        q->setXY(x,y);
        q++;
    }
    q=p;
    cout<<"共有"<display();
        q++;
    }
    cout<

对象的引用:

例题(改名字):

#include
using namespace std;
class Point {
    int x,y;
public:
    Point(int a=0,int b=0){
        x=a,y=b;
    }
    void display(){
        cout<<"("<

类的组合

组合的定义:

#include
using namespace std;
class A;
class B;
class A{
    int i;
public:
    A(int k=0){
        i=k;
        cout<<"调用了构造函数.\n";
    }
};
class B{
    A a;
    int j;
};
int main(){
    B b;
    return 0;
}

组合类的构造函数

例题:

#include
using namespace std;
class Point{
    double x,y;
public:
    Point(double a=0,double b=0){
        x=a;
        y=b;
        cout<<"调用了Point类的构造函数"<

组合类的顺序:

成员对象的初始化顺序是按照对象成员在组合类中的声明的先后顺序进行,

与成员初始化表中成员对象出现的顺序无关。

例题:

#include
using namespace std;
class A{
    int i;
public:
    A(int s=0){
        i=s;
        cout<<"A类的构造函数"<

string类:

s.length()和‘+’:

#include
#include
using namespace std;
int main()
{
    string str1="we";
    string str2=("programming");
    cout<<"programming的长度是"<

类的静态成员

例题,静态成员:

#include
using namespace std;
class Point{
    double x,y;
    static int cnt;
public:
    Point(double a=0,double b=0):x(a),y(b){
        cnt++;
    }
    ~Point(){
        cnt--;
    }
    void show(){
        cout<<"the number of Point is "<show();
    delete p;
    p1.show();
    return 0;
}

静态函数成员:

#include
#include
#include
using namespace std;
class Salesman{
    char name[10];
    int salecnt;
    double price;
    double per;
    double com;
    static int cnt;
    static double tot;
public:
    Salesman(char nm[],int Cnt,double Price,double Per){
        strcpy(name,nm);
        cnt=Cnt;
        price=Price;
        per=Per;
        com=price*cnt*per;
        tot+=com;
        cnt++;
    }static double getAver(){
        return tot/cnt;
    }static void show(){
        cout<<"共有"<

类的友元

友元函数:

例题5-19 用友元函数计算两点间的距离:

#include
#include
#include
using namespace std;
class Point{
    double x,y;
public:
    Point(double a=0,double b=0){
        x=a,y=b;
    }
    friend double dist(const Point &p1,const Point &p2);
};
double dist(const Point &p1,const Point &p2){
    double d1,d2,dist;
    d1=p1.x-p2.x;
    d2=p1.y-p2.y;
    dist=sqrt(d1*d1+d2*d2);
    return dist;
}
int main(){
    Point p1(1.0,2.0),p2(2.0,3.0);
    cout<<"两点之间的距离是:"<

常对象:

例题:

#include
using namespace std;
class Time{
    int h,mi,sec;
public:
    Time(int hr,int mnt,int scd){
        h=hr;
        mi=mnt;
        sec=scd;
    }
    void setTime(int nh,int nm,int ns);
    void show()const ;
};
void Time::setTime(int hr,int mn,int se){
    h=hr;
    mi=mn;
    sec=se;
}
void Time::show()const {
    cout<

常函数:

#include
using namespace std;
class A{
int ta;
public:
    A(int tmp=12):ta(tmp){};
    void hello ()const{
        printf("hello");
    }
    void xA()const{
        cout<











你可能感兴趣的:(C++课本例题,雄关漫道真如铁,而今C++从头阅)