Skip to content

Instantly share code, notes, and snippets.

@allen501pc
Created May 1, 2011 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allen501pc/950274 to your computer and use it in GitHub Desktop.
Save allen501pc/950274 to your computer and use it in GitHub Desktop.
[C++] Non-Friend Class
#include<iostream>
#include<cstdlib>
using namespace std;
/* Note: 這裡的CWin隱含著可以自由存取private,protected, public成員。也就是CWin Class本身就隱含著自己是自己的frined.
*
*/
class CWin
{
private:
char m_nID_;
char * m_ptrTitle_;
public:
CWin(char nID='D',char * ptrText="Default windows"): m_nID_(nID)
{
m_ptrTitle_=new char[50];
strcpy(m_ptrTitle_, ptrText);
}
void set_data(char nID,char * ptrText)
{
m_nID_ = nID;
strcpy(m_ptrTitle_, ptrText);
}
void show()
{
cout<<"Windows"<< m_nID_<<":"<< m_ptrTitle_<<endl;
}
~CWin()
{
delete [] m_ptrTitle_;
}
CWin(const CWin &win)
{
m_nID_ = win.m_nID_; // Copy ID.
strcpy(m_ptrTitle_,win.m_ptrTitle_); // Copy title.
}
void operator=(CWin &win);
};
void CWin::operator=(CWin &win)
{
m_nID_ = win.m_nID_; // Copy ID.
strcpy(m_ptrTitle_,win.m_ptrTitle_); // Copy title.
}
int main(void)
{
CWin win1('A',"Main windows"),win2;
win1.show();
win2.show();
win1=win2;
cout<<endl<<"set win1=win2 ....."<<endl;
win1.show();
win2.show();
win1.set_data('B',"Hello windows");
cout<<endl<<"change win1..."<<endl;
win1.show();
win2.show();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment