C++练习---第二天

第一篇:

代码:


#include<iostream>
using namespace std;
char * GetNum(char * src, char*buf)
{
    while(*src != '\0')
    {
        if(isdigit(*src))break;
        src++;
    }
    if(*src=='\0')
    return NULL;
    while(*src!='\0'&&isdigit(*src))
    {
        *buf = *src;
        buf++;
        src++;
    }
    *buf = '\0'
    return src;
}
int main()
{
    char str[100],digits[20];
    cin.getline(str,100);
    char *p =str;
    int i =1;
    while((p = GetNum(p,digits)) != NULL)
    {
        cout << "digits string" << i <<"is" << digits <<endl;
        i++;
    }
    return 0;
}

命令:

第二篇:

代码:

#include<iostream>
using namespace std;
const int Size = 4;
template <typename T>
class MyClass
{
public:
    MyClass(T * p)
    {
        for(int i =0;i<Size;i++)
        array[i]= p[i];
    }
    void Print();
private:
    Tarray[Size];
};
template<typename T>
void MyClass<T>::Print()
{
    for(int i =0; i< Size; i++)
    cout << array[i] <<'t';
}

int main()
{
    int intArray[Size] = (1,2,3,4);
    MyClass<int>obj(intArray);
    obj.Print();
    cout<<endl;
    return 0;
}

命令:

 第三篇:

代码:

#include<iostream>
using namespace std;
class Book
{
    char * title;
    int num_pages;
    char *writer;
public:
    Book(const char * the_title,int pages, const char * the_writer):num pages(pages)
    {
    title = new char[strlen(the_title)+1];
    strcpy(title, the_title);
    writer = new char[strlen(the_writher)+1];
    strcpy(writer,the_writer);
    }
    Book()
    {delete[]title writher;}
    int numOfPages()const{return num_pages;}
    const char* theTitle()const{return title;}
    const char * theWriter()
    const{return writer;} 
};
class TeachingMaterial: public Book
{
    char * course;
public:
    TeachingMaterial(const char * the_title,int pages,const char *the_writer,const char*the_course):Book(the_title,pages.the_writer)
    course = new char[srtlen (the_course)+1];
    strcpy(course,the_course);
}
{
    TeachingMaterial(){delete[]course;}
    const char * theCourse() const{return course;}
};
int main()
{
    TeachingMaterial a book("C++语言程序设计",299,"张三","面向对象的程序设计");
    cout << "教材名:" << a_book.theTitle() <<endl;<<"页数:"<< a_book.numOf Pages() << endl << "相关课程:" <<a_book.theCOursc()<<endl;
    cout << endl;
    return 0 ;
}

命令:

 第四篇:

#include<iostream>
using namespace std;
class MyClass
{
public:
    MyClass (int x):number(x){}
    MyClass(){}
    friend void Judge(MyClass &obj);
    private:
        int number;
};
void Judge(MyClass &obj)
{
    if(obj.number == 10)
        cout << "You are right." << endl;
        else
        cout <<"Sorry"<< endl;
}
int main()
{
    MyClass object(10);
    Judge(object);
    return 0;
}

命令:

 第五篇:

代码:

#include<iostream>
using namespace std;

class MyClass
{
public:
    MyClass(int i = 10)
    {
        value = i;cout << "Constructor called." <<endl;
    }
    MyClass(const MyClass&p)
    {
        value = p.value;
        cout << "Copy constructor called." << endl;
    }
    void Print()
    {
        cout << "The value is" << value << endl;
    }
    MyClass()
    {
        cout << "Destructor called." << endl;
    }
    private:
    int value;

};
int main()
{
    MyClass obj1;
    obj1.Print();
    MyClass obj2(obj1);
    obj2.Print();
    return 0;
}

命令:

第六篇:

代码:

 

#include<iostream>
using namespace std;
enum DOGCOLOR {BLACK, WHITE,YELLOW,BROWN,PIEBALD,OTHER};
class Dog
{
    DOGCOLOR color;
    char name[20];
    static int count;
public:
    Dog (char name[],DOGCOLOR color)
    {
        strcpy(this->name,name);
        this->color,color;
    }
    DOGCOLOR getColor() const{return color;}
    const char getName()const{return*name;}
    const char*getColorString()const
    {
        switch(color)
        {
            case BLACK: return"black";
            case WHITE: return"white";
            case YELLOW: return"brown";
            case PIEBALD: return"piebald";
        }
        return"motley";
    }
    void show()const
    {
        cout << "There is a " << getColorString() << "dog named"<< name <<'.'<<endl;
    }
};
int main()
{
    Dog dog1("Hoho",WHITE),dog2("Haha",BLACK),dog3("Hihi",OTHER);
    dog1.show();
    dog2.show();
    dog3.show();
    return 0;
}

命令:

 第七篇:

代码:

#include<iostream>
#include<cmath>
using namespace std;
class Line;
double length(Line);
class Line
{
    double x1,y1;
    double x2,y2;
public:
    Line(double x1,double y1,double x2,double y2)
    {
        this->x1 =x1;
        this->y1 =y1;
        this->x2 =x2;
        this->y2 =y2;
    }
    double getX1()const{return x1;}
    double getY1()const{return y1;}
    double getx2()const{return x2;}
    double gety2()const{retuen y2;}
    void show()const
    {
        cont << "End point1 =("<< x1 << "," << y1;cout<<"),End point 2 =("<< x2 <<","y2;cout <<"),length = "<< length(*tis)<<"0"<<endl;<<"0"<<endl;
    }
};
double length(Line 1)
{
    return sqrt((1.getX1()-1.getX2())*(1.getX1()-1.getX2())+(1.getY1()-1.getY2())*(1.getY1()-1.getY2()));
}
int main()
{
Line r1(1.0,8.0,5.0,2.0);
r1.show();
return 0;
}

命令:

 第八篇:

代码:

#include<iostream>
using namespace std;
class StudentInfo
{
protected:
    char*Name;
    int Age;
    int ID;
    int CourseNum;
    float Record;
public:
    StudentInfo(char *name,int Age,int ID,int courseNum,float record);
    StudentInfo(){}
    float AverageRecord()
    {
        return Record/CourseNum;
    }
    void show()const
    {
        cout << "Name:" << Name << "Age:" << Age << "ID:" << ID << "CourseNum:"<<CourseNum << "Record:"<<Record <<endl;
    }
};
StudentInfo::StudentInfo(char*Name,int Age,int ID,int CourseNum,float Record)
{
    Name = name;
    Age = age;
    this->ID= ID;
    CourseNum = CourseNum;
    Record = Record;
}
int main()
{
    StudentInfo st("Smit",21,99999,12,970);
    st.show();
    return 0;
}

命令:

 第九篇:

代码:

#include<iostream>
using namespace std;
class MyClass
{
    int*p;
    const int N;
public:
    MyClass(int val):N(1)
    {
        p = new int;
        *p = val;
    }
    MyClass(){delete[]p;}
    friend void print (MyClass&obj);
};
void print(MyClass&obj)
{
    cout << "value = " <<*(obj.p) << endl;
    count << "number = " <<obj.N <<endl;
}
int main()
{
    MyClass obj(63);
    print(obj);
    return 0;
}

命令:

第十篇:

代码:

#include<iostream>
using namespace std;
class Foo
{
public:
    Foo(char x){x_=x;}
    char getX()const {return x_;}
public:
    static int y_;
private:
    char x_;
};
int Foo:y_= 42;
int main(int argc,char*argv[])
{
    Foo f('a');
    cout << "X=" << f.getx()<< endl;
    cout << "Y=" << f.gety()<< endl;
    return 0;
}

命令:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值