>即時新聞-熱門

2008年8月29日星期五

vc++ - 一元運算作法

#include<iostream>
using namespace std;


class my_Score

public:
int my_var;  
my_Score()

my_var=9;

my_Score(int a)

my_var=a;

my_Score operator++(int);  // 必須定義為類別的成員函式
my_Score operator--(int);  // 必須定義為類別的成員函式
};
// 宣告++運算子多載

my_Score my_Score::operator++(int)

return my_Score(my_var++);


// 宣告--運算子多載
my_Score my_Score::operator--(int)

return my_Score(my_var--);


void main()

int i,j;
for(i=1;i<=10;i++){
my_Score st1(i)++;  
    cout << "值 :" << st1.my_var  << endl;
st1++;   


for(i=10;i>=1;i--){
my_Score st2(i);  
    cout << "值 :" << st2.my_var  << endl;
st2--;   



vc++ 多元一元載子 四則運算

#include<iostream>
using namespace std;


class my_Score

public:
int my_var;   
my_Score(int a) 

my_var=a;

};
int operator+(int x,my_Score s1)

return (x+s1.my_var);

int operator-(int x,my_Score s1)

return (x-s1.my_var);

int operator*(int x,my_Score s1)

return (x*s1.my_var);



int operator/(int x,my_Score s1)

return (x/s1.my_var);


void main()

my_Score my_st(20);  
cout << "100 + 20 = " << (100+my_st) << endl; 
cout << "100 - 20 = " << (100-my_st) << endl; 
cout << "100 * 20 = " << (100*my_st) << endl; 
cout << "100 / 20 = " << (100/my_st) << endl; 

VC++ - 多載化建構子

#include<iostream>
#include "ctype.h"
using namespace std;

class Data

public:

Data(char b)

cout << "參數為:" << (int)b << endl;


};

void main()

    
char p;
cout<<"請輸入一個字元"<<endl;
cin>>p;
    
if (isalpha(p)){
Data d1(p);

else{
cout<<"輸入的不是字元"<<endl;

vc++ - 多載函式類別成員1

#include<iostream>
using namespace std;

class Score

int china;
int english;
int math;
public:
int sum,sum1;

void total_score()

sum=0;

void total_score(int a,int b,int c)

china=a;
if (china<60)
china=60;
math=b;
if (math<60)
math=60;
english=c;
if (english<60)
english=60;
sum1=china+english+math;
sum=sum1+sum;

};

void main()
{   
int x1,x2,x3;
char d;
Score s1;
s1.total_score();
for(;;){

cout<<"請輸入國文成績 : ";
cin>>x1;
cout<<"請輸入英文成績 : ";
cin>>x2;
cout<<"請輸入數學成績 : ";
cin>>x3;
   s1.total_score(x1,x2,x3);
cout << "合計是" << s1.sum << endl;
cout<<"請問是否要繼續:是 -> y , 否-> n ";
cin>>d;
if (d=='N'||d=='n')
break;}


vc++ - 多載函式類別成員

#include<iostream>
using namespace std;

class Score

int china;
int english;
int math;
public:
int sum,sum1;

void total_score()


sum=0;

void total_score(int a,int b,int c)

china=a;
math=b;
english=c;
sum1=china+english+math;
sum=sum1+sum;

};

void main()
{   
int x1,x2,x3;
char d;
Score s1;
s1.total_score();
for(;;){

cout<<"請輸入國文成績 : ";
cin>>x1;
cout<<"請輸入英文成績 : ";
cin>>x2;
cout<<"請輸入數學成績 : ";
cin>>x3;
   s1.total_score(x1,x2,x3);
cout << "合計是" << s1.sum << endl;
cout<<"請問是否要繼續:是 -> y , 否-> n ";
cin>>d;
if (d=='N'||d=='n')
break;}


2008年8月27日星期三

vc++ - 多載函式2

#include<iostream>
#include "math.h"
using namespace std;


int multisum(int a,int b)  

int sum=0;
sum=a*b;
return sum;



int multisum(int a,int b,int c) 

int sum=0;
sum=a*b*c;
return sum;

float multisum(float a) 

float sum=0;
sum=a*a*3.1415962;
return sum;


void main()

int w,l,h;
float r;

    cout << "請輸入長:" << endl;
cin>>l;
 cout << "請輸入寬:" << endl;
cin>>w;
 cout << "請輸入高:" << endl;
cin>>h;
r = (float)sqrt(w*w+l*l)/2;

    cout << "圓面積:" << multisum(r) << endl;
cout << "面積:" << multisum(w,l) << endl;
cout << "體積:" << multisum(w,l,h) << endl;
};

vc++ - 多載函式1

#include<iostream>
using namespace std;


int multisum(int a,int b)  

int sum=0;
sum=a*b;
return sum;



int multisum(int a,int b,int c) 

int sum=0;
sum=a*b*c;
return sum;

float multisum(int a) 

float sum=0;
sum=a*a*3.1415962;
return sum;


void main()

    cout << "圓面積:" << multisum(12) << endl;
cout << "面積:" << multisum(12,12) << endl;
cout << "體積:" << multisum(3,4,5) << endl;
};

vc++ - 多重繼承3

#include<iostream>
using namespace std;


class my_no

private:
int no;
public:
void set_no(int no)

this->no=no;

int get_no()

return no;

};

class my_score

private:
int china;
int english;
int math;
public:

void set_china(int china)

this->china=china;

int get_china()

return china;


void set_english(int english)

this->english=english;

int get_english()

return english;


void set_math(int math)

this->math=math;

int get_math()

return math;

};
class my_student : public my_no,public my_score

private:
int sum;
public:
my_student()

int sum=0;

void show()

cout << "學號:" << get_no() << endl;
cout << "國文:" << get_china() << endl;
cout << "英文:" << get_english() << endl;
cout << "數學:" << get_math() << endl;

void add1()


sum=get_china()+get_english()+get_math();
cout << "總成績為:" << sum << endl;

};
void main()


my_student st1;
int s1,s2,s3;

st1.set_no(3);
cout << "請輸入國文成績:";
cin >> s1;
cout << "請輸入英文成績:";
cin >> s2;
cout << "請輸入數學成績:";
cin >> s3;

st1.set_china(s1);
st1.set_english(s2);
st1.set_math(s3);
cout << "*************************************" << endl;


st1.show();
st1.add1();

vc++ - 多重繼承2

#include<iostream>
using namespace std;
class my_score

private:
int china;
int english;
int math;
public:
void set_china(int china)
{ this->china=china; }
int get_china()
{ return china; }
void set_english(int english)
{ this->english=english; }
int get_english()
{ return english; }
void set_math(int math)
{ this->math=math; }
int get_math()
{ return math; }
};

class my_student : public my_score

private:
int sum;
float avg;
public:
my_student() 

{ int sum=0;
float avg=0.0;

void show_score()

cout << "國文:" << get_china() << endl;
cout << "英文:" << get_english() << endl;
cout << "數學:" << get_math() << endl;

void add1()

sum=get_china()+get_english()+get_math();

cout << "總成績為:" << sum << endl;

void avg1()


avg=(float)(get_china()+get_english()+get_math())/3;

cout << "總平均為:" << avg << endl;

};
void main()


my_student st1;
int s1,s2,s3;


cout << "請輸入國文成績:";
cin >> s1;
cout << "請輸入英文成績:";
cin >> s2;
cout << "請輸入數學成績:";
cin >> s3;

st1.set_china(s1);
st1.set_english(s2);
st1.set_math(s3);
cout << "************************************" << endl;
st1.show_score();
st1.add1();
st1.avg1();

vc++ - 多重繼承

#include<iostream>
using namespace std;
class my_score

private:
int china;
int english;
int math;
public:
void set_china(int china)
{ this->china=china; }
int get_china()
{ return china; }
void set_english(int english)
{ this->english=english; }
int get_english()
{ return english; }
void set_math(int math)
{ this->math=math; }
int get_math()
{ return math; }
};
class my_student : public my_score

private:
int sum;
float avg;
public:
my_student() { int sum=0;
float avg=0.0;

void show_score()

cout << "國文:" << get_china() << endl;
cout << "英文:" << get_english() << endl;
cout << "數學:" << get_math() << endl;

void add1()

sum=get_china()+get_english()+get_math();
avg=(float)sum/3;
cout << "總成績為:" << sum << endl;
cout << "總平均為:" << avg << endl;
}};
void main()


my_student st1;
int s1,s2,s3;


cout << "請輸入國文成績:";
cin >> s1;
cout << "請輸入英文成績:";
cin >> s2;
cout << "請輸入數學成績:";
cin >> s3;
st1.set_china(s1);
st1.set_english(s2);
st1.set_math(s3);
cout << "***********************" << endl;
st1.show_score();
st1.add1();

vc++ - protected作法

#include<iostream>
using namespace std;
class my_data

private:
int no;
protected:
int china;
int narth;
public:
int math;
int english;
my_data()  

no=0;
china=0;
narth=0;
math=0;
english=0;

void set_no(int n1)
{ no=n1; }
void show_no()
{ cout << "學號 : " << no << endl; }
};
class my_data1:protected my_data

public:
void set_data1(int n3,int n4)

china=n3;   
narth=n4;

void set_data2(int n5 , int n6)

english=n5;  
math=n6;

void setno(int n7)


set_no(n7); 

void show_data()


show_no();
cout << "國文 :" << china << endl;
cout << "自然 :" << narth << endl;
cout << "英文 :" << english << endl;
cout << "數學 :" << math << endl;

};
void main()

 int i,a,no[sizeof(int)],ch[sizeof(int)],ma[sizeof(int)],en[sizeof(int)],na[sizeof(int)];
  cout << "請輸入同學位數 :" ;
cin>>a ;
 for(i=0;i<a;i++){
        cout << "第 "<<i+1<<"位同學" ;
    cout << "學號 :" ;
cin>>no[i] ;
     cout << "國文 :" ;
cin>>ch[i] ;
cout << "自然 :" ;
cin>>na[i];
cout << "英文 :" ;
cin>>en[i];

cout << "數學 :" ;
cin>>ma[i];
 }
my_data1 obj1;

for(i=0;i<3;i++){
obj1.setno(no[i]); 
obj1.set_data1(ch[i],na[i]);
obj1.set_data2(en[i],ma[i]);
obj1.show_data();

VC++ - 繼承

#include<iostream>
using namespace std;
class my_score

public:
int china;
int math;
int english;
    int total;

my_score();  
void get_score();
void show_score();
void gettotal();

};
my_score::my_score()

china=0;  
math=0; 
english=0;
total =china+math+english;

void my_score::get_score()

cout << "輸入國文成績:";
cin >> china;
    cout << endl<<"輸入英文成績:";
cin >> english;
cout <<endl<< "輸入數學成績:";
cin >> math;

void my_score::show_score()

cout << "國文:" << china << endl;
    cout << "英文:" << english << endl;
cout << "數學:" << math << endl;

void my_score::gettotal()

total =china+math+english;



class st1:public my_score

private:
int no;
char name[20];
public:
void show_data();
void set_data();
void show_total();

};
void st1::show_data()

cout << "********************************" << endl;
show_score();
cout << "座號:" << no << endl;
cout << "姓名:" << name << endl;

void st1::set_data()

cout << "輸入座號:";
cin >> no;
cout << "輸入姓名:";
cin >> name;




void st1::show_total()

   
cout << "合計 :" << total  << endl;


void main()

st1 s1;

s1.get_score();
s1.set_data();

s1.gettotal();
s1.show_total();
s1.show_data();

2008年8月26日星期二

VC++ - 例外(感謝PETER)

#include<iostream>
using namespace std;

void main()
{ 

 cout<<"例外"<<endl;

 try
 {

 int n1;
 cout<<"請輸入除數:";
 cin>>n1;  //經過我無聊而且有耐心的精神發現到,n1宣告int後,經cin指另寫進去n1的是ASSIC CODE十進位碼。^^//

if (!(n1>0 && n1<=9))  //我只修改這行判別式,意思就是說不是輸入0—9的一律都是"除零例外"不管是符號,英文字或是組合鍵,通通擋住,滴水不漏。 ^^//
 {
 throw 1;  
 } 
 cout<<"不是例外"<<endl; 
 
 }

 catch(int e)  
 {
 cout<<"除零例外"<<endl;
 }

2008年8月25日星期一

vc++ - 例外 字母

#include<iostream>
using namespace std;

void main()

{ 
cout<<"例外"<<endl;

try


char x;
cout<<"請輸入除數:";
cin>>x; 



if (x>='A' && x<='Z'){
throw 1;  }
 
cout<<"不是例外"<<endl; 


catch(int r)  

cout<<"除零例外"<<endl;

VC++ - 類別相減

#include <iostream.h>
class Addsum

int x;
 public:
    Addsum(int);
void sum(Addsum);   
void show();
};
Addsum::Addsum(int s)
{ x=s;}
void Addsum::sum(Addsum obj)
{ x-=obj.x;  }
void Addsum::show()
{ cout<<x;}
void main()

int x,y;
int*p =&x;
int*p1=&y;
cout<<"請輸入數值一"<<endl;
cin>>x;
cout<<"請輸入數值二"<<endl;
cin>>y;
Addsum a(*p),b(*p1);
a.show();
cout<<"-";
b.show();
cout<<"=";
a.sum(b);   
a.show();
cout<<endl;

類別 - 最大值

#include<iostream>

using namespace std;

class basedata {
private:
char no[6];     
char name[20];  
int math;
int china;  
    int english; 
int scoresum(void);
float scoreavg(void);
int scoremax(void);
public:
void inputbasedata();
void showbasedata();
};
void basedata::inputbasedata(void)  

cout << "****************************" << endl;
         cout << "學號:" ;
cin >> no;
cout <<endl<< "姓名:";
cin >> name ;
            cout <<endl<< "數學:";
cin >> math;
cout <<endl<< "國文:";
cin >> china;
cout <<endl<< "英文:";
cin >> english;



void basedata::showbasedata(void)  
 { 
int sum;
   float avg;
   int mymax;
   sum=scoresum();
   avg=scoreavg();
   mymax=scoremax();


cout << "***************************" << endl;

cout << "學號:" << no << endl;
cout << "姓名:" << name << endl;
cout << "數學:" << math << endl;
cout << "國文:" << china << endl;
cout << "英文:" << english << endl;
cout << "***************************" << endl;
              cout << "總分:" << sum<< endl;
         cout << "平均:" << avg << endl;
cout << "最大值:" << mymax << endl;

int basedata::scoresum()  
{   
int my_sum;
   my_sum=math+china+english;
   return my_sum;



float basedata::scoreavg()  
{   
float my_avg;
   my_avg=(math+china+english)/3;
   return my_avg;



int basedata::scoremax()  
{   
int my_max;
if (math > china){
if (math >english){
   my_max=math;

else{
my_max=english;


else{
if (china >english){
   my_max=china;

else{
my_max=english;



  
   return my_max;



void main()

basedata a[4];
int i;
cout<<"請輸入資料"<<endl;
for(i=0;i<4;i++){

      a[i].inputbasedata();

cout<<"資料輸出"<<endl;
for(i=0;i<4;i++){
a[i].showbasedata();

類別 - 加入平均合計

#include<iostream>

using namespace std;

class basedata {
private:
char no[6];     
char name[20];  
int math;
int china;  
    int english; 
int scoresum(void);
float scoreavg(void);
public:
void inputbasedata();
void showbasedata();
};
void basedata::inputbasedata(void)  

cout << "****************************" << endl;
         cout << "學號:" ;
cin >> no;
cout <<endl<< "姓名:";
cin >> name ;
            cout <<endl<< "數學:";
cin >> math;
cout <<endl<< "國文:";
cin >> china;
cout <<endl<< "英文:";
cin >> english;



void basedata::showbasedata(void)  
 { 
int sum;
   float avg;
   sum=scoresum();
   avg=scoreavg();

cout << "***************************" << endl;

cout << "學號:" << no << endl;
cout << "姓名:" << name << endl;
cout << "數學:" << math << endl;
cout << "國文:" << china << endl;
cout << "英文:" << english << endl;
cout << "***************************" << endl;
              cout << "總分:" << sum<< endl;
         cout << "平均:" << avg << endl;

int basedata::scoresum()  
{   
int my_sum;
   my_sum=math+china+english;
   return my_sum;



float basedata::scoreavg()  
{   
float my_avg;
   my_avg=(math+china+english)/3;
   return my_avg;




void main()

basedata a[4];
int i;
cout<<"請輸入資料"<<endl;
for(i=0;i<4;i++){

      a[i].inputbasedata();

cout<<"資料輸出"<<endl;
for(i=0;i<4;i++){
a[i].showbasedata();

2008年8月24日星期日

VC++ - 樣版 - 類別

#include <iostream>
using namespace std;

class AddValue {
  int i;
public:
  AddValue(int b1) 
  {
  for(i=0;i<b1;i++)
  i=b1+1; 
  }
  friend void F1(const AddValue&);
};
 
void F1(const AddValue& p1) {  
  cout << p1.i << endl; 

 
void main()

int y;
cout<<"請輸入數值:";
cin>>y;
  F1(y); 
 

VC++ - 樣版 - 明顯特殊化

#include <cstring>
#include <iostream>
using namespace std;
 
template<typename T> const T& 
min(const T& a, const T& b) 

if (a < b) 
return a;
else
return b;

 
template<>  
const int* const& min<const int*>(const int* const& a,const int* const& b) 

if (a < b) 
return a;
else
return b;



void main() 

  int s1,s2; 
   cout << "請輸入數值1 : ";
   cin >>s1;
cout  << endl << "請輸入數值2 : ";
   cin >>s2;
  cout  << endl << "最小值為:"<<min(s1, s2) << endl;

VC++ - 樣版函式

#include <iostream>
using namespace std;

template<class T>  
void ListArray(const T a1,int count)  

   for (int i = 0; i < count; i++ )
      cout<<a1[i]<<" ";
   cout << endl;


void main()

   int y  ;  
   char a[5],b[6]="abcde"; 
   cout << "陣列內容為:" << endl;
 for ( y = 0; y < 5; y++ )
      a[y] = b[y];

   ListArray(a,2);   
   ListArray(b,5);

VC++ - 多載一元運算子

#include<iostream>
using namespace std;


class my_Score

public:
int my_var;   
my_Score(int a) 

my_var=a;

};


int operator+(int x,my_Score s1)

return (x+s1.my_var);


void main()

         int y;
         cout << "請輸入一個數值: " <<endl;
         cin>>y;
my_Score my_st(y);  
cout <<"100 +"<< y<< "=" << (100+my_st) << endl; 

VC++ - 後置式多載

#include<iostream>
using namespace std;

class my_Score

public:
int my_var;  
my_Score()

my_var=88;

my_Score(int a)

my_var=a;

my_Score operator++(int);  
my_Score operator--(int);
};

my_Score my_Score::operator++(int)

return my_Score(my_var++);


my_Score my_Score::operator--(int)

return my_Score(my_var--);


void main()

my_Score st1(3);  
my_Score st2;      
st1++;          
st2--;


cout << "第一次" << st1.my_var  << endl;
cout << "第二次" << st2.my_var  << endl;

 
妹咕數位學園歡迎網友們來信指教 妹咕信箱