c++初级 案例分享
1.栈区
# include <iostream>
using namespace std;
int * func ( int b)
{
b= 100 ;
int a= 10 ;
return & a;
}
int main ( )
{
int * p= func ( 1 ) ;
cout<< * p<< endl;
cout<< * p<< endl;
system ( "pause" ) ;
return0;
}
2.堆区
# include <iostream>
using namespace std;
int * func ( )
{
int * p= new int ( 10 ) ;
return p;
}
int main ( )
{
int * p= func ( ) ;
cout<< * p<< endl;
system ( "pause" ) ;
return 0 ;
}
new运算符
# include <iostream>
using namespace std;
int * func ( )
{
int * p= new int ( 10 ) ;
return p;
}
void test01 ( )
{
int * p= func ( ) ;
cout<< * p<< endl;
cout<< * p<< endl;
cout<< * p<< endl;
delete p;
}
void test02 ( )
{
int * arr= new int [ 10 ] ;
for ( int i= 0 ; i< 10 ; i++ )
{
arr[ i] = i+ 100 ;
}
for ( int i= 0 ; i< 10 ; i++ )
{
cout<< arr[ i] << endl;
}
delete [ ] arr;
}
int main ( )
{
test01 ( ) ;
test02 ( ) ;
system ( "pause" ) ;
return 0 ;
}
类1
# include <iostream>
using namespace std;
class Date
{
public :
void setDate ( int y, int m, int d) ;
void showDate ( ) ;
private :
int year, month, day;
} ;
void Date :: setDate ( int y, int m, int d)
{
year= y;
month= m;
day= d;
}
void Date :: showDate ( )
{
cout<< month<< "-" << day<< "-" << year<< endl;
}
int main ( )
{
Date D;
int year, month, day;
cout<< "输入日期" ;
cin>> year>> month>> day;
D. setDate ( year, month, day) ;
D. showDate ( ) ;
return 0 ;
}
c++递归求模
# include <iostream>
using namespace std;
int fn ( int n)
{
int m= 1 ;
for ( int i= 1 ; i<= n; i++ )
{
m*= i;
}
return m;
}
int main ( )
{
int n;
int sum= 0 ;
cout<< "请输出n的值:" ;
cin>> n;
for ( int i= 1 ; i<= n; i++ )
{
sum+= fn ( i) ;
}
cout<< "1!+2!+3!+...n!=" << sum<< endl;
return 0 ;
}
不同类型度数转换
# include <iostream>
using namespace std;
int fn ( int a)
{
int b;
b= 5.0 / 9 * ( a- 32 ) ;
return b;
}
int main ( )
{
int a, b;
cout<< "请输入度数" << endl;
cin >> a;
b= fn ( a) ;
cout<< "对应的摄氏温度为" << b<< endl;
return 0 ;
system ( "pause" ) ;
}
类之基本信息
# include <iostream>
# include <string>
using namespace std;
class Student
{
public :
void setStudent ( int n, char na[ ] , int a, char addr[ ] ) ;
void showStudent ( ) ;
private :
int no;
char name[ 20 ] ;
int age;
char address[ 50 ] ;
} ;
void Student :: setStudent ( int n, char na[ ] , int a, char addr[ ] )
{
no= n;
strcpy ( name, na) ;
age= a;
strcpy ( address, addr) ;
}
void Student :: showStudent ( )
{
cout<< no<< " " << name<< " " << age<< " " << address<< endl;
}
int main ( )
{
Student std;
std. setStudent ( 1 , "Mary" , 20 , "江西省南昌市" ) ;
std. showStudent ( ) ;
return 0 ;
}
关于继承
# include <iostream>
using namespace std;
class Base
{
public :
int m_A;
protected :
private :
int m_C;
} ;
class Son : public Base
{
public :
int m_D;
} ;
void test01 ( )
{
cout<< "size of son=" << sizeof ( Son) << endl;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
新增string类型
# include <iostream>
using namespace std;
int main ( )
{
string str2= "hello world" ;
cout<< str2<< endl;
system ( "pause" ) ;
return 0 ;
}
水仙花数
# include <iostream>
using namespace std;
int main ( )
{
int num= 100 ;
do
{
int a= 0 ;
int b= 0 ;
int c= 0 ;
a= num% 10 ;
b= num/ 10 % 10 ;
c= num/ 100 ;
if ( a* a* a+ b* b* b+ c* c* c== num)
{
cout<< num<< endl;
}
num++ ;
}
while ( num< 1000 ) ;
system ( "pause" ) ;
return 0 ;
}
类与对象初始化
# include <iostream>
using namespace std;
class Person
{
public :
Person ( int a, int b, int c) : m_A ( a) , m_B ( b) , m_C ( c)
{
}
int m_A;
int m_B;
int m_C;
} ;
void test01 ( )
{
Person p ( 30 , 20 , 10 ) ;
cout<< "m_A=" << p. m_A<< endl;
cout<< "m_B=" << p. m_B<< endl;
cout<< "m_C=" << p. m_C<< endl;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
类对象作为类成员
# include <iostream>
using namespace std;
# include <string>
class Phone
{
public :
Phone ( string name)
{
m_PhoneName= name;
cout<< "phone构造" << endl;
}
~ Phone ( )
{
cout<< "Phone析构" << endl;
}
string m_PhoneName;
} ;
class Person
{
public :
Person ( string name, string pName) : m_Name ( name) , m_Phone ( pName)
{
cout<< "Person构造" << endl;
}
~ Person ( )
{
cout<< "Person析构" << endl;
}
void playGame ( )
{
cout<< m_Name<< "使用" << m_Phone. m_PhoneName<< "牌手机" << endl;
}
string m_Name;
Phone m_Phone;
} ;
void test01 ( )
{
Person p ( "张三" , "苹果max" ) ;
p. playGame ( ) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
关于友元
# include <iostream>
using namespace std;
# include <string>
class Building
{
friend void goodGay ( Building * building) ;
public :
Building ( )
{
m_SittingRoom= "客厅" ;
m_BedRoom= "卧室" ;
}
public :
string m_SittingRoom;
private :
string m_BedRoom;
} ;
void goodGay ( Building * building)
{
cout<< "好基友的全局函数 正在访问" << building-> m_SittingRoom<< endl;
cout<< "好基友的全局函数 正在访问" << building-> m_BedRoom<< endl;
}
void test01 ( )
{
Building building;
goodGay ( & building) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
指针与数组
# include <iostream>
using namespace std;
int main ( )
{
int arr[ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ;
cout<< "第一个元素为:" << arr[ 0 ] << endl;
int * p= arr;
cout<< "利用指针访问第一个元素:" << * p<< endl;
p++ ;
cout<< "利用指针访问第二个元素" << * p<< endl;
cout<< "利用指针便历数组" << endl;
int * p2= arr;
cout<< * p2<< endl;
for ( int i= 0 ; i< 10 ; i++ )
{
cout<< arr[ i] << endl;
}
system ( "pause" ) ;
return 0 ;
}
类易错
# include <iostream>
# include <string>
using namespace std;
class Student
{
public :
void setStudent ( int n, char na[ ] , int a, char add[ ] ) ;
void showStudent ( ) ;
private :
int no;
char name[ 20 ] ;
int age;
char address[ 50 ] ;
} ;
void Student :: setStudent ( int n, char na[ ] , int a, char add[ ] )
{
no= n;
strcpy ( name, na) ;
age= a;
strcpy ( address, add) ;
}
void Student :: showStudent ( )
{
cout<< no<< name<< age<< address<< endl;
}
int main ( )
{
Student D;
D. setStudent ( 1 , "mary" , 18 , "江西省南昌市" ) ;
D. showStudent ( ) ;
system ( "pause" ) ;
return 0 ;
}
复制构造函数的实现
# include <iostream>
using namespace std;
class Point
{
public :
Point ( int xx= 0 , int yy= 0 ) { x= xx; y= yy; }
Point ( const Point& p) ;
~ Point ( ) ;
void setX ( int xx) { x= xx; }
void setY ( int yy) { y= yy; }
int getX ( ) { return x; }
int getY ( ) { return y; }
private :
int x, y;
} ;
Point :: Point ( const Point& p)
{
x= p. x;
y= p. y;
cout<< "调用复制构造函数!" << endl;
}
Point :: ~ Point ( )
{
cout<< "调用析构函数!" << endl;
}
void fun1 ( Point p)
{
cout<< p. getX ( ) << endl;
}
Point fun2 ( )
{
Point a ( 1 , 2 ) ;
return a;
}
int main ( )
{
Point A ( 4 , 5 ) ;
Point B= A;
cout<< B. getX ( ) << endl;
fun1 ( B) ;
B= fun2 ( ) ;
cout<< B. getX ( ) << endl;
system ( "pause" ) ;
return 0 ;
}
结构体
# include <iostream>
using namespace std;
# include <string>
struct Student
{
string name;
int age;
int score;
} ;
int main ( )
{
struct Student s1;
s1. name= "张三" ;
s1. age= 20 ;
s1. score= 100 ;
cout<< "姓名" << s1. name<< "年龄" << s1. age<< "成绩" << s1. score<< endl;
}
拷贝类
# include <iostream>
using namespace std;
class Person
{
public :
Person ( )
{
cout<< "Person默认构造函数调用" << endl;
}
Person ( int age)
{
cout<< "Person有参构造函数调用" << endl;
m_Age= age;
}
Person ( const Person & p)
{
cout<< "Person拷贝构造函数调用" << endl;
m_Age= p. m_Age;
}
~ Person ( )
{
cout<< "Person析构函数调用" << endl;
}
int m_Age;
} ;
void text01 ( )
{
Person p1 ( 20 ) ;
Person p2 ( p1) ;
cout<< "p2的年龄为" << p2. m_Age<< endl;
}
void doWork ( Person p)
{
}
void text02 ( )
{
Person p;
doWork ( p) ;
}
Person doWork2 ( )
{
Person p1;
cout<< ( int * ) & p1<< endl;
return p1;
}
void text03 ( )
{
Person p= doWork2 ( ) ;
cout<< ( int * ) & p<< endl;
}
int main ( )
{
text03 ( ) ;
system ( "pause" ) ;
return 0 ;
}
浅拷贝 深拷贝
# include <iostream>
using namespace std;
class Person
{
public :
Person ( )
{
cout<< "Person的默认构造函数调用" << endl;
}
Person ( int age, int height)
{
m_Age= age;
m_Height= new int ( height) ;
cout<< "Person的有参构造函数调用" << endl;
}
Person ( const Person & p)
{
cout<< "Person拷贝构造函数调用" << endl;
m_Age= p. m_Age;
m_Height= p. m_Height;
m_Height= new int ( * p. m_Height) ;
}
~ Person ( )
{
if ( m_Height != NULL )
{
delete m_Height;
m_Height= NULL ;
}
cout<< "Person的析构构造函数调用" << endl;
}
int m_Age;
int * m_Height;
} ;
void text01 ( )
{
Person p1 ( 18 , 160 ) ;
cout<< "p1的年龄为" << p1. m_Age<< "身高为" << * p1. m_Height<< endl;
Person p2 ( p1) ;
cout<< "p2的年龄为" << p2. m_Age<< "身高为" << * p2. m_Height<< endl;
}
int main ( )
{
text01 ( ) ;
system ( "pause" ) ;
}
常函数 常变量
# include <iostream>
using namespace std;
class Person
{
public :
void showPerson ( ) const
{
this -> m_B= 100 ;
}
void func ( )
{
}
int m_A;
mutable int m_B;
} ;
void test01 ( )
{
Person p;
p. showPerson ( ) ;
cout<< p. m_B<< endl;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
指针 通透
# include <iostream>
using namespace std;
int main ( )
{
int a= 10 ;
int * p;
p= & a;
cout<< "a的地址为" << & a<< endl;
cout<< "指针p为" << p<< endl;
* p= 1000 ;
cout<< "a=" << a<< endl;
cout<< "*p=" << * p<< endl;
system ( "pause" ) ;
return 0 ;
}
指针大小相等
# include <iostream>
using namespace std;
int main ( )
{
int a= 10 ;
int * p;
p= & a;
cout<< "sizeof(int*)=" << sizeof ( int * ) << endl;
cout<< "sizeof(int*)=" << sizeof ( float * ) << endl;
cout<< "sizeof(int*)=" << sizeof ( char * ) << endl;
cout<< "sizeof(int*)=" << sizeof ( double * ) << endl;
system ( "pause" ) ;
return 0 ;
}
关于空指针
# include <iostream>
using namespace std;
int main ( )
{
int * p= NULL ;
cout<< * p;
system ( "pause" ) ;
return 0 ;
}
值传递 地址传递
# include <iostream>
using namespace std;
void swap01 ( int a, int b)
{
int temp= a;
a= b;
b= temp;
cout<< a<< endl;
cout<< b<< endl;
}
void swap02 ( int * p1, int * p2)
{
int temp= * p1;
* p1= * p2;
* p2= temp;
}
int main ( )
{
int a= 10 , b= 20 ;
swap02 ( & a, & b) ;
cout<< a<< endl;
cout<< b<< endl;
}
冒泡法
# include <iostream>
using namespace std;
void bubbleSort ( int * arr, int len)
{
for ( int i= 0 ; i< len- 1 ; i++ )
{
for ( int j= 0 ; j< len- i- 1 ; j++ )
{
if ( arr[ j] > arr[ j+ 1 ] )
{
int temp= arr[ j] ;
arr[ j] = arr[ j+ 1 ] ;
arr[ j+ 1 ] = temp;
}
}
}
}
void printArray ( int * arr, int len)
{
for ( int i= 0 ; i< len; i++ )
cout<< arr[ i] << endl;
}
int main ( )
{
int arr[ 10 ] = { 4 , 3 , 6 , 9 , 1 , 2 , 10 , 8 , 7 , 5 } ;
int len= sizeof ( arr) / sizeof ( arr[ 0 ] ) ;
bubbleSort ( arr, len) ;
printArray ( arr, len) ;
cout<< * arr<< endl;
system ( "pause" ) ;
return 0 ;
}
类的访问权限
# include <iostream>
using namespace std;
class Base
{
public :
static int a;
protected :
static int b;
private :
static int c;
} ;
int Base:: a= 10 ;
int Base:: b= 20 ;
int Base:: c= 30 ;
class Derived : public Base
{
public :
void show ( )
{
cout<< a<< endl;
cout<< b<< endl;
}
} ;
class DDerived : public Derived
{
public :
void show ( )
{
cout<< Base:: a<< endl;
cout<< Base:: b<< endl;
}
} ;
int main ( )
{
Derived d;
DDerived dd;
d. show ( ) ;
dd. show ( ) ;
return 0 ;
}
还是结构体
# include <iostream>
using namespace std;
# include <string>
struct student
{
string name;
int age;
int score;
} stu3;
int main ( )
{
struct student stu1;
stu1. name= "张三" ;
stu1. age= 18 ;
stu1. score= 100 ;
cout<< "年龄" << stu1. age<< "姓名" << stu1. name<< "学分" << stu1. score<< endl;
struct student stu2= { "李四" , 19 , 60 } ;
cout<< "年龄" << stu2. age<< "姓名" << stu2. name<< "学分" << stu2. score<< endl;
stu3. name= "王五" ;
stu3. age= 60 ;
stu3. score= 59 ;
cout<< "年龄" << stu3. age<< "姓名" << stu3. name<< "学分" << stu3. score<< endl;
}
结构体数组
# include <iostream>
using namespace std;
# include <string>
struct Student
{
string name;
int age;
int score;
} ;
Student stuArray[ 3 ] =
{
{ "张三" , 18 , 100 } ,
{ "李四" , 28 , 99 } ,
{ "王五" , 38 , 66 }
} ;
int main ( )
{
stuArray[ 2 ] . name= "赵六" ;
stuArray[ 2 ] . age= 80 ;
stuArray[ 2 ] . score= 60 ;
for ( int i= 0 ; i< 3 ; i++ )
{
cout<< "姓名" << stuArray[ i] . name
<< "年龄:" << stuArray[ i] . age
<< "学分" << stuArray[ i] . score<< endl;
}
system ( "pause" ) ;
return 0 ;
}
类做友元
# include <iostream>
using namespace std;
# include <string>
class Building ;
class Building
{
friend class GoodGay ;
public :
Building ( ) ;
public :
string m_SittingRoom;
private :
string m_BedRoom;
} ;
class GoodGay
{
public :
GoodGay ( ) ;
void visit ( ) ;
Building * building;
} ;
Building :: Building ( )
{
m_SittingRoom= "客厅" ;
m_BedRoom= "卧室" ;
}
GoodGay :: GoodGay ( )
{
building= new Building;
}
void GoodGay :: visit ( )
{
cout<< "好基友类正在访问" << building-> m_SittingRoom<< endl;
cout<< "好基友类正在访问" << building-> m_BedRoom<< endl;
}
void test01 ( )
{
GoodGay gg;
gg. visit ( ) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
运算符重载
# include <iostream>
using namespace std;
class Person
{
public :
Person operator + ( Person & p)
{
Person temp;
temp. m_A= this -> m_A+ p. m_A;
temp. m_B= this -> m_B+ p. m_B;
return temp;
}
int m_A;
int m_B;
} ;
Person operator + ( Person & p1, Person & p2)
{
Person temp;
temp. m_A= p1. m_A+ p2. m_A;
temp. m_B= p1. m_B+ p2. m_B;
return temp;
}
void test01 ( )
{
Person p1;
p1. m_A= 10 ;
p1. m_B= 10 ;
Person p2;
p2. m_A= 10 ;
p2. m_B= 10 ;
Person p3= operator + ( p1, p2) ;
cout<< "p3.m_A=" << p3. m_A<< endl;
cout<< "p3.m_B=" << p3. m_B<< endl;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
左移运算符重载
# include <iostream>
using namespace std;
class Person
{
friend ostream& operator << ( ostream & cout, Person & p) ;
public :
Person ( int a, int b)
{
m_A= a;
m_B= b;
}
private :
int m_A;
int m_B;
} ;
ostream& operator << ( ostream & cout, Person & p)
{
cout<< "m_A" << p. m_A<< "m_B=" << p. m_B;
return cout;
}
void test01 ( )
{
Person p ( 10 , 10 ) ;
cout<< p<< "hello word" << endl;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
重载递增
# include <iostream>
using namespace std;
class MyInteger
{
friend ostream& operator << ( ostream& cout, MyInteger myint) ;
public :
MyInteger ( )
{
m_Num= 0 ;
}
MyInteger& operator ++ ( )
{
m_Num++ ;
return * this ;
}
MyInteger operator ++ ( int )
{
MyInteger temp= * this ;
m_Num++ ;
return temp;
}
private :
int m_Num;
} ;
ostream& operator << ( ostream& cout, MyInteger myint)
{
cout<< myint. m_Num;
return cout;
}
void test01 ( )
{
MyInteger myint;
cout<< ++ ( ++ myint) << endl;
}
void test02 ( )
{
MyInteger myint;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
重载
# include <iostream>
using namespace std;
# include <string>
class Person
{
public :
Person ( string name, int age)
{
m_Name= name;
m_Age= age;
}
bool operator == ( Person & p)
{
if ( this -> m_Name== p. m_Name && this -> m_Age== p. m_Age)
{
return true ;
}
return false ;
}
bool operator != ( Person & p)
{
if ( this -> m_Name== p. m_Name&& this -> m_Age== p. m_Age)
{
return false ;
}
return true ;
}
string m_Name;
int m_Age;
} ;
void test01 ( )
{
Person p1 ( "Tom" , 18 ) ;
Person p2 ( "Tom" , 18 ) ;
if ( p1== p2)
{
cout<< "p1与p2是相等的" << endl;
}
else
cout<< "p1和p2是不相等的" << endl;
if ( p1!= p2)
{
cout<< "p1与p2是不相等的" << endl;
}
else
cout<< "p1和p2是相等的" << endl;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
函数调用运算符重载
# include <iostream>
using namespace std;
# include <string>
class MyPrint
{
public :
void operator ( ) ( string test)
{
cout<< test<< endl;
}
} ;
void MyPrint02 ( string test)
{
cout<< test<< endl;
}
void test01 ( )
{
MyPrint myPrint;
myPrint ( "hello world" ) ;
MyPrint02 ( "hello world" ) ;
}
class MyAdd
{
public :
int operator ( ) ( int num1, int num2)
{
return num1+ num2;
}
} ;
void test02 ( )
{
MyAdd myadd;
int ret= myadd ( 100 , 100 ) ;
cout<< ret<< endl;
}
int main ( )
{
test01 ( ) ;
test02 ( ) ;
system ( "pause" ) ;
return 0 ;
}
构造与析构顺序
# include <iostream>
using namespace std;
class Base
{
public :
Base ( )
{
cout<< "Base构造函数" << endl;
}
~ Base ( )
{
cout<< "Base的析构函数" << endl;
}
protected :
private :
} ;
class Son : public Base
{
public :
Son ( )
{
cout<< "Son构造函数" << endl;
}
~ Son ( )
{
cout<< "Son的析构函数" << endl;
}
protected :
private :
} ;
void test01 ( )
{
Base b;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
空指针可以调用成员函数
# include <iostream>
using namespace std;
class Person
{
public :
void showClassName ( )
{
cout<< "this is Person class" << endl;
}
void showPersonAge ( )
{
if ( this == NULL )
{
return ;
}
cout<< "age=" << this -> m_Age<< endl;
}
int m_Age;
} ;
void test01 ( )
{
Person * p= NULL ;
p-> showClassName ( ) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
}
常量指针 指针常量
# include <iostream>
using namespace std;
int main ( )
{
int a= 10 ;
int b= 10 ;
const int * p= & a;
p= & b;
int * const p2= & a;
* p2= 100 ;
const int * const p3= & a;
system ( "pause" ) ;
return 0 ;
}
this 指针
# include <iostream>
using namespace std;
class Person
{
public :
Person ( int age)
{
this -> age= age;
}
Person& PersonAddAge ( Person & p)
{
this -> age += p. age;
return * this ;
}
int age;
} ;
void test01 ( )
{
Person p1 ( 18 ) ;
cout<< "p1的年龄为" << p1. age<< endl;
}
void test02 ( )
{
Person p1 ( 10 ) ;
Person p2 ( 10 ) ;
p2. PersonAddAge ( p1) . PersonAddAge ( p1) . PersonAddAge ( p1) ;
cout<< "p2的年龄为" << p2. age<< endl;
}
int main ( )
{
test01 ( ) ;
test02 ( ) ;
system ( "pause" ) ;
return 0 ;
}
逆序排序
# include <iostream>
using namespace std;
int main ( )
{
int arr[ 5 ] = { 1 , 2 , 6 , 7 , 8 } ;
for ( int i= 0 ; i< 5 ; i++ )
cout<< "逆序前的排序为" << arr[ i] << endl;
int temp;
int start= 0 ;
int end;
end= sizeof ( arr) / sizeof ( arr[ 0 ] ) - 1 ;
while ( start< end)
{
temp= arr[ start] ;
arr[ start] = arr[ end] ;
arr[ end] = temp;
start++ ;
end-- ;
}
cout<< "逆序后的排列为" << endl;
for ( int i= 0 ; i< 5 ; i++ )
{
cout<< arr[ i] << endl;
}
system ( "pause" ) ;
}
敲桌子小游戏
# include <iostream>
using namespace std;
int main ( )
{
for ( int i= 1 ; i<= 100 ; i++ )
{ cout<< i<< endl;
if ( ( i% 7 == 0 ) || ( i% 10 == 7 ) || ( i/ 10 == 7 ) )
{
cout<< "敲桌子" << endl;
}
}
system ( "pause" ) ;
return 0 ;
}
结构体小案例
# include <iostream>
using namespace std;
# include <string>
# include <ctime>
struct Student
{
string sName;
int score;
} ;
struct Teacher
{
string tName;
struct Student sArray[ 5 ] ;
} ;
void allocateSpace ( struct Teacher tArray[ ] , int len)
{
string nameSeed= "ABCDE" ;
for ( int i= 0 ; i< len; i++ )
{
tArray[ i] . tName= "Teacher_" ;
tArray[ i] . tName+= nameSeed[ i] ;
for ( int j= 0 ; j< 5 ; j++ )
{
tArray[ i] . sArray> [ j] . sName= "Student_" ;
tArray[ i] . sArray> [ j] . sName+= nameSeed[ j] ;
int random= rand ( ) % 61 + 40 ;
tArray. sArray[ j] . score= random;
}
}
}
void orintInfo ( struct Teacher [ ] , int len)
{
for ( int i= 0 ; i< len; i++ )
{
cout<< "老师的姓名" << tArray[ i] . tName<< endl;
for ( int j= 0 ; j< 5 ; j++ )
{
cout<< "\t学生姓名" << tArray[ i] . sArray[ j] . sName<<
"考试分数" << tArray[ i] . sArray[ j] . score<< endl;
}
}
}
int main ( )
{
srand ( ( unsigned int ) time ( NULL ) ) ;
struct Teacher tArray[ 3 ] ;
int len= sizeof ( tArray) / sizeof ( tArray[ 0 ] ) ;
allocateSpace ( tArray, len) ;
printInfo ( tArray, len) ;
system ( "pause" ) ;
return 0 ;
}
引用
# include <iostream>
using namespace std;
int main ( )
{
int a= 10 ;
int & b= a;
int c= 20 ;
b= c;
cout<< a<< b<< c;
system ( "pause" ) ;
return 0 ;
}
多态
# include <iostream>
using namespace std;
class Animal
{
public :
virtual void speak ( )
{
cout<< "动物在说话" << endl;
}
} ;
class Cat : public Animal
{
public :
void speak ( )
{
cout<< "小猫在说话" << endl;
}
} ;
class Dog : public Animal
{
public :
void speak ( )
{
cout<< "小狗在说话" << endl;
}
} ;
void doSpeak ( Animal & animal)
{
animal. speak ( ) ;
}
void test01 ( )
{
Cat cat;
doSpeak ( cat) ;
Dog dog;
doSpeak ( dog) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
多态2-煮茶
# include <iostream>
using namespace std;
class AbstructDrinking
{
public :
virtual void Boil ( ) = 0 ;
virtual void Brew ( ) = 0 ;
virtual void PourInCup ( ) = 0 ;
virtual void PutSomething ( ) = 0 ;
void makeDrink ( )
{
Boil ( ) ;
Brew ( ) ;
PourInCup ( ) ;
PutSomething ( ) ;
}
} ;
class Coffee : public AbstructDrinking
{
public :
void Boil ( )
{
cout<< "煮水" << endl;
}
virtual void Brew ( )
{
cout<< "冲泡咖啡" << endl;
}
virtual void PourInCup ( )
{
cout<< "倒入杯中" << endl;
}
virtual void PutSomething ( )
{
cout<< "加入糖和牛奶" << endl;
}
} ;
class Tea : public AbstructDrinking
{
public :
virtual void Boil ( )
{
cout<< "煮矿泉水" << endl;
}
virtual void Brew ( )
{
cout<< "冲茶叶" << endl;
}
virtual void PourInCup ( )
{
cout<< "倒入玻璃杯中" << endl;
}
virtual void PutSomething ( )
{
cout<< "加入柠檬" << endl;
}
} ;
void doWork ( AbstructDrinking * abs)
{
abs-> makeDrink ( ) ;
delete abs;
}
void test01 ( )
{
doWork ( new Coffee) ;
cout<< "----------" << endl;
doWork ( new Tea) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
多态-电脑组装
# include <iostream>
using namespace std;
class CPU
{
public :
virtual void calculate ( ) = 0 ;
} ;
class VideoCard
{
public :
virtual void display ( ) = 0 ;
} ;
class Memory
{
public :
virtual void storage ( ) = 0 ;
} ;
class Computer
{
public :
Computer ( CPU * cpu, VideoCard* vc, Memory * mem)
{
m_cpu= cpu;
m_vc= vc;
m_mem= mem;
}
void work ( )
{
m_cpu-> calculate ( ) ;
m_vc-> display ( ) ;
m_mem-> storage ( ) ;
}
~ Computer ( )
{
if ( m_cpu != NULL )
{
delete m_cpu;
m_cpu= NULL ;
}
if ( m_vc != NULL )
{
delete m_vc;
m_vc= NULL ;
}
if ( m_vc != NULL )
{
delete m_mem;
m_mem= NULL ;
}
}
private :
CPU* m_cpu;
VideoCard * m_vc;
Memory* m_mem;
} ;
class IntelCPU : public CPU
{
public :
virtual void calculate ( )
{
cout<< "Inter的cpu开始计算了" << endl;
}
} ;
class IntelVideoCard : public VideoCard
{
public :
virtual void display ( )
{
cout<< "Inter的显卡开始显示了" << endl;
}
} ;
class IntelMemory : public Memory
{
public :
virtual void storage ( )
{
cout<< "Inter的内存条开始显示了" << endl;
}
} ;
class LenovoCPU : public CPU
{
public :
virtual void calculate ( )
{
cout<< "Lenovo的cpu开始计算了" << endl;
}
} ;
class LenovoVideoCard : public VideoCard
{
public :
virtual void display ( )
{
cout<< "Lenovo的显卡开始显示了" << endl;
}
} ;
class LenovoMemory : public Memory
{
public :
virtual void storage ( )
{
cout<< "Lenovo的内存条开始显示了" << endl;
}
} ;
void test01 ( )
{
CPU* intelCpu= new IntelCPU;
VideoCard * intelCard= new IntelVideoCard;
Memory * intelMem= new IntelMemory;
Computer * computer1= new Computer ( intelCpu, intelCard, intelMem) ;
computer1-> work ( ) ;
delete computer1;
cout<< "------------" << endl;
cout<< "第二台电脑开始工作" << endl;
Computer * computer2= new Computer ( new LenovoCPU, new LenovoVideoCard, new LenovoMemory) ;
computer2-> work ( ) ;
delete computer2;
cout<< "第三条电脑的组装" << endl;
Computer * computer3= new Computer ( new IntelCPU, new LenovoVideoCard, new IntelMemory) ;
computer3-> work ( ) ;
delete computer3;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
文件操作
# include <iostream>
using namespace std;
# include <fstream>
int main ( )
{
ofstream ofs;
ofs. open ( "test.txt" , ios:: out) ;
ofs<< "姓名:张三" << endl;
ofs<< "姓别:男" << endl;
ofs<< "年龄:18" << endl;
ofs. close ( ) ;
system ( "pause" ) ;
return 0 ;
}
读文件
# include <iostream>
using namespace std;
# include <fstream>
# include <string>
void test01 ( )
{
ifstream ifs;
ifs. open ( "test.txt" , ios:: in) ;
if ( ! ifs. is_open ( ) )
{
cout<< "文件打开失败" << endl;
return ;
}
char buf[ 1024 ] = { 0 } ;
while ( ifs>> buf)
{
cout<< buf<< endl;
}
ifs. close ( ) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
案例文件写
# include <iostream>
using namespace std;
# include <fstream>
int main ( )
{
ofstream gjl;
gjl. open ( "miji.txt" , ios:: out) ;
gjl<< "秘籍一:无敌" << endl;
gjl<< "秘籍二;无限体力" << endl;
gjl<< "秘籍三:无限火龙" << endl;
gjl. close ( ) ;
system ( "pause" ) ;
return 0 ;
}
案例文件读
# include <iostream>
using namespace std;
# include <fstream>
# include <string>
void test01 ( )
{
ifstream ifs;
ifs. open ( "miji.txt" , ios:: in) ;
if ( ! ifs. is_open ( ) )
{
cout<< "文件输出失败" << endl;
return ;
}
char buf[ 1024 ] = { 0 } ;
while ( ifs>> buf)
{
cout<< buf<< endl;
}
ifs. close ( ) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
模板1
# include <iostream>
using namespace std;
# include <string>
template < class NameType , class AgeType >
class Person
{
public :
Person ( NameType name, AgeType age)
{
this -> m_Name= name;
this -> m_Age= age;
}
void showPerson ( )
{
cout<< "name=" << this -> m_Name<< "age:" << this -> m_Age<< endl;
}
NameType m_Name;
AgeType m_Age;
} ;
void test01 ( )
{
Person< string, int > p1 ( "孙悟空" , 999 ) ;
p1. showPerson ( ) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
模板2
# include <iostream>
using namespace std;
void swapNum ( int & a, int & b)
{
int temp= a;
a= b;
b= temp;
}
void swapDouble ( double & a, double & b)
{
double temp= a;
a= b;
b= temp;
}
template < typename T >
void mySwap ( T& a, T& b)
{
T temp= a;
a= b;
b= temp;
}
void test01 ( )
{
int a= 10 ;
int b= 20 ;
mySwap < int > ( a, b) ;
cout<< "a=" << a<< "b=" << b<< endl;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
模板注意事项
# include <iostream>
using namespace std;
template < typename T >
void mySwap ( T& a, T& b)
{
T temp= a;
a= b;
b= temp;
}
void test01 ( )
{
int a= 10 ;
int b= 20 ;
char c= 'c' ;
mySwap ( a, b) ;
cout<< "a=" << a<< endl;
cout<< "b=" << b<< endl;
}
template < class T >
void fun ( )
{
cout<< "fun调用" << endl;
}
void test02 ( )
{
fun < int > ( ) ;
}
int main ( )
{
test01 ( ) ;
test02 ( ) ;
system ( "pause" ) ;
return 0 ;
}
函数模板
# include <iostream>
using namespace std;
template < class T >
void mySwap ( T& a, T& b)
{
T temp= a;
a= b;
b= temp;
}
template < class T >
void mySort ( T arr[ ] , int len)
{
for ( int i= 0 ; i< len; i++ )
{
int max= i;
for ( int j= i+ 1 ; j< len; j++ )
{
if ( arr[ max] < arr[ j] )
{
max= j;
}
}
if ( max!= i)
{
mySwap ( arr[ max] , arr[ i] ) ;
}
}
}
template < class T >
void printArray ( T arr[ ] , int len)
{
for ( int i= 0 ; i< len; i++ )
{
cout<< arr[ i] << " " ;
}
cout<< endl;
}
void test01 ( )
{
char charArr[ ] = "badcef" ;
int num= sizeof ( charArr) / sizeof ( char ) ;
mySort ( charArr, num) ;
printArray ( charArr, num) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
普通函数与函数模板的区别
模板规则
# include <iostream>
using namespace std;
void myPrint ( int a, int b)
{
cout<< "调用普通函数" << endl;
}
template < class T >
void myPrint ( T a, T b)
{
cout<< "调用的模板" << endl;
}
template < class T >
void myPrint ( T a, T b, T c)
{
cout<< "调用的重载的模板" << endl;
}
void test01 ( )
{
int a= 10 ;
int b= 20 ;
myPrint ( a, b, 100 ) ;
char c1= 'a' ;
char c2= 'b' ;
myPrint ( c1, c2) ;
myPrint < > ( a, b) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
模板的局限性
# include <iostream>
using namespace std;
# include <string>
class Person
{
public :
Person ( string name, int age)
{
this -> m_Name= name;
this -> m_Age= age;
}
public :
string m_Name;
int m_Age;
} ;
template < class T >
bool myCompare ( T & a, T & b)
{
if ( a== b)
{
return true ;
}
else
{
return false ;
}
}
template < > bool myCompare ( Person & p1, Person & p2)
{
if ( p1. m_Name== p2. m_Name&& p1. m_Age== p2. m_Age)
{
return true ;
}
else
{
return false ;
}
}
void test02 ( )
{
Person p1 ( "tom" , 12 ) ;
Person p2 ( "tom" , 12 ) ;
bool ret= myCompare ( p1, p2) ;
if ( ret)
{
cout<< "p1==p2" << endl;
}
else
{
cout<< "p1!=p2" << endl;
}
}
int main ( )
{
test02 ( ) ;
system ( "pause" ) ;
return 0 ;
}
类模板与函数模板的区别
# include <iostream>
using namespace std;
# include <string>
template < class NameType , class AgeType = int >
class Person
{
public :
Person ( NameType name, AgeType age)
{
this -> m_Name= name;
this -> m_Age= age;
}
void showPerson ( )
{
cout<< "name=" << this -> m_Name<< endl;
cout<< "age=" << this -> m_Age<< endl;
}
NameType m_Name;
AgeType m_Age;
} ;
void test01 ( )
{
Person< string, int > p ( "孙悟空" , 1000 ) ;
p. showPerson ( ) ;
}
void test02 ( )
{
Person< string> p ( "猪八戒" , 999 ) ;
p. showPerson ( ) ;
}
int main ( )
{
void test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
成员函数创建时期
# include <iostream>
using namespace std;
class Person1
{
public :
void showPerson1 ( )
{
cout<< "Person1 show" << endl;
}
} ;
class Person2
{
public :
void showPerson2 ( )
{
cout<< "Person2 show" << endl;
}
} ;
template < class T >
class MyClass
{
public :
T obj;
void fun1 ( )
{
obj. showPerson1 ( ) ;
}
void fun2 ( )
{
obj. showPerson2 ( ) ;
}
} ;
void test01 ( )
{
MyClass< Person1> m;
m. fun1 ( ) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
类模板成员函数类外实现
# include <iostream>
using namespace std;
# include <string>
template < class T1 , class T2 >
class Person
{
public :
Person ( T1 name, T2 age) ;
void showPerson ( ) ;
T1 m_Name;
T2 m_Age;
} ;
template < class T1 , class T2 >
Person < T1, T2> :: Person ( T1 name, T2 age)
{
this -> m_Name= name;
this -> m_Age= age;
}
template < class T1 , class T2 >
void Person < T1, T2> :: showPerson ( )
{
cout<< "姓名:" << this -> m_Name<< "年龄:" << this -> m_Age<< endl;
}
void test01 ( )
{
Person< string, int > P ( "Tom" , 20 ) ;
P. showPerson ( ) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
类模板与继承
# include <iostream>
using namespace std;
template < class T >
class Base
{
T m;
} ;
class son : public Base < int >
{
} ;
void test01 ( )
{
son s1;
}
template < class T1 , class T2 >
class son2 : public Base < T2 >
{
public :
son2 ( )
{
cout<< "T1的类型为:" << typeid ( T1) . name ( ) << endl;
cout<< "T2的类型为:" << typeid ( T2) . name ( ) << endl;
}
T1 obj;
} ;
void test02 ( )
{
son2< int , char > s2;
}
int main ( )
{
test02 ( ) ;
system ( "pause" ) ;
return 0 ;
}
设计一个卡车类
# include <iostream>
using namespace std;
class Vehicle
{
public :
virtual void showinfo ( ) = 0 ;
protected :
char Name[ 20 ] ;
} ;
class Car : public Vehicle
{
public :
Car ( char * name)
{
strcpy ( Name, name) ;
}
void showinfo ( )
{
cout << Name << endl;
}
protected :
int Radius;
} ;
class Truck : public Vehicle
{
public :
Truck ( char * name)
{
strcpy ( Name, name) ;
}
void showinfo ( ) { cout << Name << endl; }
} ;
class Boat : public Vehicle
{
public :
Boat ( char * name)
{
strcpy ( Name, name) ;
}
void showinfo ( ) { cout << Name << endl; }
} ;
int main ( )
{
Vehicle* vp;
Car car ( "奔驰" ) ;
Truck truck ( "运输卡车" ) ;
Boat boat ( "游艇" ) ;
vp = & car;
vp-> showinfo ( ) ;
vp = & truck;
vp-> showinfo ( ) ;
vp = & boat;
vp-> showinfo ( ) ;
return 0 ;
}