生活随笔
收集整理的這篇文章主要介紹了
C++使用类和对象(谭浩强9.8-9.14)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
例9.8 對象的引用
#include <iostream>
using namespace std
;
class Time
{
public : Time ( int , int , int ) ; int hour
; int minute
; int sec
;
} ;
Time
:: Time ( int h
, int m
, int s
)
{ hour
= h
; minute
= m
; sec
= s
;
}
void fun ( Time
& t
)
{ t
. hour
= 18 ;
}
int main ( )
{ Time
t1 ( 10 , 13 , 56 ) ; fun ( t1
) ; cout
<< t1
. hour
<< endl
; return 0 ;
}
程序執行結果如圖:
例9.9 對象的賦值和復制
#include <iostream>
using namespace std
;
class Box
{
public : Box ( int = 10 , int = 10 , int = 10 ) ; int volumn ( ) ;
private : int height
; int width
; int length
;
} ;
Box
:: Box ( int h
, int w
, int l
)
{ height
= h
; width
= w
; length
= l
;
}
int Box
:: volumn ( )
{ return ( height
* width
* length
) ;
}
int main ( )
{ Box
box1 ( 15 , 30 , 25 ) , box2
; cout
<< "The volumn of box1 is" << box1
. volumn ( ) << endl
; box2
= box1
; cout
<< "The volumn of box2 is" << box2
. volumn ( ) << endl
;
}
執行結果如圖: 例10 靜態數據成員(可以通過對象名引用,也可以通過類名引用)
#include <iostream>
using namespace std
;
class Box
{
public :
Box ( int , int ) ;
int volumn ( ) ;
static int height
;
int width
;
int length
;
} ;
Box
:: Box ( int w
, int l
)
{ width
= w
; length
= l
;
}
int Box
:: volumn ( )
{ return ( height
* width
* length
) ;
}
int Box
:: height
= 10 ;
int main ( )
{ Box
a ( 15 , 20 ) , b ( 20 , 30 ) ; cout
<< a
. height
<< endl
; cout
<< b
. height
<< endl
; cout
<< Box
:: height
<< endl
; cout
<< "The volumn of box1 is" << a
. volumn ( ) << endl
; return 0 ;
}
程序執行效果如圖: 例9.11靜態成員函數:統計學生平均成績
#include <iostream>
using namespace std
;
class Student
{
public : Student ( int n
, int a
, float s
) : num ( n
) , age ( a
) , score ( s
) { } void total ( ) ; static float average ( ) ;
private : int num
; int age
; float score
; static float sum
; static int count
;
} ;
void Student
:: total ( )
{ sum
+ = score
; count
++ ;
}
float Student
:: average ( )
{ return ( sum
/ count
) ;
}
float Student
:: sum
= 0 ;
int Student
:: count
= 0 ;
int main ( )
{ Student stud
[ 3 ] = { Student ( 1001 , 18 , 70 ) , Student ( 1002 , 19 , 78 ) , Student ( 1005 , 20 , 98 ) } ; int n
; cout
<< "please input the number of students:" ; cin
>> n
; for ( int i
= 0 ; i
< n
; i
++ ) stud
[ i
] . total ( ) ; cout
<< "the average score of" << n
<< "student is" << Student
:: average ( ) << endl
; return 0 ;
}
程序執行效果如圖: 例9.13:友元成員函數:
#include <iostream>
using namespace std
;
class Date ;
class Time
{
public : Time ( int , int , int ) ; void display ( Date
& ) ;
private : int hour
; int minute
; int sec
;
} ;
class Date
{
public : Date ( int , int , int ) ; friend void Time
:: display ( Date
& ) ;
private : int month
; int day
; int year
;
} ;
Time
:: Time ( int h
, int m
, int s
)
{ hour
= h
; minute
= m
; sec
= s
;
}
void Time
:: display ( Date
& d
)
{ cout
<< d
. month
<< "/" << d
. day
<< "/" << d
. year
<< "/" << endl
; cout
<< hour
<< ":" << minute
<< ":" << sec
<< endl
;
}
Date
:: Date ( int m
, int d
, int y
)
{ month
= m
; day
= d
; year
= y
;
}
int main ( )
{ Time
t1 ( 10 , 13 , 56 ) ; Date
d1 ( 12 , 25 , 2004 ) ; t1
. display ( d1
) ; return 0 ;
}
程序執行結果如圖: 9.14類模板的使用:
#include <iostream>
using namespace std
;
template < class numtype >
class Compare
{
public : Compare ( numtype a
, numtype b
) { x
= a
; y
= b
; } numtype
max ( ) { return ( x
> y
) ? x
: y
; } numtype
min ( ) { return ( x
< y
) ? x
: y
; }
private : numtype x
, y
;
} ;
int main ( )
{ Compare
< int > cmp1 ( 3 , 7 ) ; cout
<< cmp1
. max ( ) << "is the Maximun of two integer numbers." << endl
; cout
<< cmp1
. min ( ) << "is the Minimun of two integer numbers." << endl
<< endl
; Compare
< float > cmp2 ( 45.78 , 93.6 ) ; cout
<< cmp2
. max ( ) << "is the Maximun of two float numbers." << endl
; cout
<< cmp2
. min ( ) << "is the Minimun of two float numbers." << endl
<< endl
; Compare
< char > cmp3 ( 'a' , 'A' ) ; cout
<< cmp3
. max ( ) << "is the Maximun of two characters." << endl
; cout
<< cmp3
. min ( ) << "is the Minimun of two characters." << endl
<< endl
; return 0 ;
}
執行結果如圖:
總結
以上是生活随笔 為你收集整理的C++使用类和对象(谭浩强9.8-9.14) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。