首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++的两个标题

2012-09-05 
c++的两个题目1、继承与派生类的应用(1)定义一个日期(年、月、日)的类和一个时间(时、分、秒)的类,并由这两个类

c++的两个题目
1、继承与派生类的应用
(1)定义一个日期(年、月、日)的类和一个时间(时、分、秒)的类,并由这两个类派生出日期和时间类。要求在主函数中完成基类和派生类的测试工作。


2、运算符重载的基本设计
(1)定义一个复数类,通过重载运算符:“*、/”,直接实现二个复数之间的乘除运算。
编写一个完整的程序,测试重载运算符的正确性。要求乘法“*”用友元函数实现重载,除法“/”用成员函数实现重载。

谁能把这两个题目解答一下呢?

[解决办法]
自己做吧 一些简单的作业题 都不想做了
[解决办法]

C/C++ code
//function Data.h#ifndef DATA_H#define DATA_Hclass Data{public:    Data( int = 1,int = 1,int = 1900 );    void setMonth( int );    void setDay( int );    void setYear( int );    void nextDay();    int getMonth() const;    int getDay() const;    int getYear() const;    void displayDate() const;    int checkDay( int );private:    int month;    int day;    int year;    };#endif//time.h#ifndef TIME_H#define TIME_H#include<iostream>using namespace std;class Time{public:    Time( int = 0,int = 0 ,int = 0 );    void setTime( int ,int ,int );    void setHour( int );    void setMinute( int );    void setSecond( int );    int getHour();    int getMinute();    int getSecond();    void printMilitary();    void printStandard();    void tick();private:    int hour;    int minute;    int second;};#endif#ifndef DT_H#define DT_H#include "data.h"#include "time.h"class DT : public Time, public Data{public:    DT(int m = 1,int d = 1,int y = 1900,int hr = 0,int min = 0,int sec = 0 ): Time(hr,min,sec),Data(m,d,y)    {}    void printMilitary();    void printStandard();    void tick();};#endif //function  Data.cpp#include <iostream>using namespace std;#include "data.h"Data::Data( int m,int d,int y){    month = 1;    day =1;    year = 1900;    setMonth( m );    setDay( d );    setYear( y );}void Data::setMonth( int m ){    if( m < 1 || m > 12 )    {        //cout<<"ERROR! enter wrong Month!"<<endl;        month = 1;        return ;    }    else        month = m;}void Data::setDay( int d ){    if( d < 1 || d > 31 )    {        //cout<<"ERROR! enter wrong Month!"<<endl;        day = 1;        return ;    }    else        day = checkDay( d );}void Data::setYear( int y ){    if( y < 1900 || y > 2010 )    {        //cout<<"ERROR! enter wrong Month!"<<endl;        year = 1900;        return ;    }    else        year = y;}int Data::getMonth() const{    return month;}int Data::getDay() const{    return day;}int Data::getYear() const{    return year;}void Data::displayDate() const{    cout<<"Date is: "<<getMonth()<<"/"<<getDay()<<"/"<<getYear()<<endl;}int Data::checkDay( int d){    static int daysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};    if( month != 2 )    {        if( d > daysPerMonth[month] || d < 0 )        {            //cout<<"ERROR! enter day wrong!"<<endl;            return 1;        }        else return d;    }    else    {        int days;        days = ( year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28);        if( d > 0 && d <= days )            return d;        else         {            //cout<<"ERROR! enter day wrong!"<<endl;            return 1;        }    }    return 1;}void Data::nextDay(){    setDay( getDay() + 1 );    if( getDay() == 1 )    {        setMonth( getMonth() + 1 );        if( getMonth() == 1 )            setYear( getYear() +1 );    }}//function time.cpp#include<iostream>using namespace std;#include"time.h"Time::Time(int hr,int min,int sec){    hour=( hr >= 0 && hr < 24 ) ? hr : 0;    minute=( min >= 0 && min < 60 ) ? min : 0;    second=( sec >= 0 && sec < 60 ) ? sec : 0;}void Time::setTime(int h,int m,int s){    hour = ( h >= 0 && h<24 ) ? h : 0;    minute = ( m >= 0 && m < 60 ) ? m : 0;    second = ( s >= 0 && s < 60 ) ? s : 0;}void Time::setHour(int h){    hour = ( h >= 0 && h < 24 ) ? h : 0;}void Time::setMinute(int m){    minute = ( m >= 0 && m < 60 ) ? m : 0;}void Time::setSecond(int s){    second = ( s >= 0 && s < 60 ) ? s : 0;}int Time::getHour(){    return hour;}int Time::getMinute(){    return minute;}int Time::getSecond(){    return second;}void Time::printMilitary(){    cout << ( hour < 10 ? "0" : "" )<< hour << ":"         << ( minute < 10 ? "0" : "" ) << minute << ":"         << ( second < 10 ? "0" : "" ) << second;}void Time::printStandard(){    cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":"         << ( minute < 10 ? "0" : "" ) << minute << ":"         << ( second < 10 ? "0" : "" ) << second         << ( hour < 12 ? "AM" : "PM" );}void Time::tick(){    setSecond( ( getSecond() + 1 ) % 60 );    if ( getSecond() == 0 )     {        setMinute( ( getMinute() + 1 ) % 60 );        if ( getMinute() == 0 )            setHour( ( getHour() + 1 ) % 24 );    }}#include "dt.h"#include <iostream>using namespace std;void DT::printMilitary(){        cout<<(getHour()<10?"0":"")<<getHour()<<" : "        <<(getMinute()<10?"0":"")<<getMinute()<<" : "        <<(getSecond()<10?"0":"")<<getSecond()<<" : "        <<getMonth()<<"/"        <<getDay()<<"/"        <<getYear()<<endl;}void DT::printStandard(){    cout<<((getHour()==0||getHour()==12)?12:getHour()%12)<<" : "        <<(getMinute()<10?"0":"")<<getMinute()<<" : "        <<(getSecond()<10?"0":"")<<getSecond()<<" "        <<(getHour()<12?"AM":"PM")<<" "        <<getMonth()<<"/"<<getDay()<<"/"<<getYear()<<endl;}void DT::tick(){    setSecond( ( getSecond() + 1 ) % 60 );    if ( getSecond() == 0 )     {        setMinute( ( getMinute() + 1 ) % 60 );        if ( getMinute() == 0 )        {            setHour( ( getHour() + 1 ) % 24 );            if( getHour() == 0 )                nextDay();        }    }}#include "dt.h"#include <iostream>using namespace std;int main(){    DT dataandtime;    dataandtime.printMilitary();    return 0;} 


[解决办法]

C/C++ code
/* 假设你已经知道了 复数的数学性质 */#include <iostream>using namespace std;class Complex {    double re, im;public:    Complex(): re(0), im(0) {}    Complex(double r): re(r), im(0) {}    Complex(double r, double i): re(r), im(i) {}    friend Complex operator*(Complex, Complex);    Complex operator/(Complex);    void display(void) { cout << re<<"+"<<im<<"i"<<endl; }};Complex operator *(Complex c1, Complex c2){    Complex ret;    ret.re = c1.re*c2.re - c1.im*c2.im;    ret.im = c1.re*c2.im + c1.im*c2.re;    return ret;}Complex Complex::operator/(Complex c){    Complex ret;    double z = c.re*c.re + c.im*c.im;    /* z != 0*/    ret.re = (re*c.re + im*c.im)/z;    ret.im = (im*c.re - re*c.im)/z;    return ret;}int main(){    Complex c1(2, 3), c2(1, 4);    Complex c3 = c1/c3; c3.display();    return 0;}
[解决办法]
不好意思 那里是 c3 = c1/c2;
[解决办法]
这是第二题
C/C++ code
#include <iostream>using namespace std;class Complex{public:    Complex():re(0),im(0){}    Complex(double r,double i):re(r),im(i){}    friend Complex operator*(Complex&,Complex&);    Complex operator/(Complex&);    void display(void)    {        cout<<"("<<re<<","<<im<<")"<<endl;    }private:    double re;    double im;};Complex Complex::operator /(Complex& c1){    Complex c2;    double d;    d = c1.re * c1.re + c1.im * c1.im;    c2.re = (c1.re * re + c1.im * im) / d;    c2.im = (c1.im * re - c1.re * im) / d;    return c2;}Complex operator* (Complex& c1,Complex& c2){    Complex c3;    c3.re = c1.re * c2.re - c1.im * c2.im;    c3.im = c1.re * c2.im + c1.im * c2.re;    return c3;    }void main(){    Complex a(1,2),b(2,4),c,d;    c = a * b;    d = b / a;    c.display();    d.display();}
[解决办法]
探讨
第二个不做了 没时间 自己解决吧 很简单的

热点排行