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();
}
}