面向对象程序设计——类的练习

C++类的实例练习。

/**
 * Personal Music Manage System
 *
 * @Author Weiqing Jin <jinmmd@gmail.com>
 * @Version 2011-12-13 19:31:13 Mumu.
 */
#include <iostream> 
#include <cstdlib>
#include <string>
#include <fstream>
#include <iomanip> 
using namespace std;
//声明Music类
class Music
{
    public:
		Music();
        Music(char* pName, char* pArtist, char* pAlbum, char* pType, float pSize, int pHits);
		Music(Music& pSong);
        ~Music();
		void setSong(char* pName, char* pArtist, char* pAlbum, char* pType, float pSize, int pHits);
        int getId();
        char* getName();
        char* getArtist();
        char* getAlbum();
        char* getType();
        float getSize();
        int getHits();
        void setId(int pId);
        void setName(char* pName);
        void setArtist(char* pArtist);
        void setAlbum(char* pAlbum);
        void setType(char* pType);
        void setSize(float pSize);
        void setHits(int pHits);
        void printInfo();
    private:
        int id;
        char* name;
        char* artist;
        char* album;
        char* type;
        float size;
        int hits;
        static int totalnum;
        static int autoInsert();
};
int Music::totalnum = 0;
//定义构造函数
Music::Music()
{}
Music::Music(char* pName, char* pArtist, char* pAlbum, char* pType, float pSize, int pHits)
{
    id = autoInsert();
    //setId(int pId);
    setName(pName);
    setArtist(pArtist);
    setAlbum(pAlbum);
    setType(pType);
    setSize(pSize);
    setHits(pHits);
}

Music::Music(Music& pSong)
{
	cout<<"Deep copy constructing..."<<endl;
	id = pSong.id;
	setName(pSong.name);
    setArtist(pSong.artist);
    setAlbum(pSong.album);
    setType(pSong.type);
    setSize(pSong.size);
    setHits(pSong.hits);
}
void Music::setSong(char* pName, char* pArtist, char* pAlbum, char* pType, float pSize, int pHits)
{
    id = autoInsert();
    //setId(int pId);
    setName(pName);
    setArtist(pArtist);
    setAlbum(pAlbum);
    setType(pType);
    setSize(pSize);
    setHits(pHits);
}
//定义析构函数

Music::~Music()
{
    cout<<"Deconstruct one song."<<endl;
    delete[] name;
    delete[] artist;
    delete[] album;
    delete[] type;
    totalnum--;
}

//函数功能:id自增
int Music::autoInsert()
{
	return ++totalnum;
}
//获取私有成员值
int Music::getId()
{
    return id;
}
char* Music::getName()
{
    return name;
}
char* Music::getArtist()
{
    return artist;
}
char* Music::getAlbum()
{
    return album;
}
char* Music::getType()
{
    return type;
}
float Music::getSize()
{
    return size;
}
int Music::getHits()
{
    return hits;
}
//设置私有成员值
void Music::setId(int pId)
{
    id = pId;
}
void Music::setName(char* pName)
{
    name = new char[strlen(pName)+1];
    if(name != 0)
        strcpy(name, pName);
}
void Music::setArtist(char* pArtist)
{
    artist = new char[strlen(pArtist)+1];
    if(artist != 0)
        strcpy(artist, pArtist);
}
void Music::setAlbum(char* pAlbum)
{
    album = new char[strlen(pAlbum)+1];
    if(album != 0)
        strcpy(album, pAlbum);
}
void Music::setType(char* pType)
{
    type = new char[strlen(pType)+1];
    if(type != 0)
        strcpy(type, pType);
}
void Music::setSize(float pSize)
{
    size = pSize;
}
void Music::setHits(int pHits)
{
    hits = pHits;
}
//打印song所有成员值
void Music::printInfo()
{
    cout<<id<<setw(10)<<name<<"."<<type<<setw(8)<<artist<<setw(28)<<album<<setw(8)<<size<<"Mb"<<setw(6)<<hits<<endl;
}
//测试
void main()
{
    int i;
    Music songs[3];
    songs[0].setSong("小情歌", "苏打绿", "小宇宙", "mp3", 10.3f, 1051);
    songs[1].setSong("情歌", "梁静茹", "别再为他流泪", "mp3", 9.8f, 954);
    songs[2].setSong("那些年", "胡夏", "那些年,我们一起追的女孩", "mp3", 8.7f, 816);
    for(i = 0; i < 3; i++)
    {
        songs[i].printInfo();
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值