1、矩阵转换题
#include<iostream>
using namespace std;
int main()
{
int a1[4][3],a2[3][4];
int i, j,b,c; cout << "原数组元素为:";
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
{cin >> a1[j][i];a2[i][j]=a1[j][i];}
cout << "倒数后数组元素如下:" << endl;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
cout << a2[j][i] << " ";
cout << endl;
}
system("pause");
return 0;
}
2、计算成绩题
#include<iostream>
#pragma warning(disable:4996)
using namespace std;
int main()
{int s1[10];
int y, i, min; int max = 0;
int c=1;
int d=1;
int a=0;
cout << "请输入考生成绩";
for (i=0,y=0; i<10; i++,y++)
{ cin>>s1[i];
if(s1[i] <0)
break;
else
a=a+s1[i];}
float z=a;
for (i = 0; i < y - 1; i++)
{if (s1[i] > max)
max = s1[i];}
min = max;
for (i = y - 1; i >= 0; i--)
{if (s1[i] <= min)
min = s1[i];
else continue;}
cout<<"输出学生成绩"<<endl;
for (i=0; i<y; i++)
{cout<<s1[i]<<", ";}
cout<<"\n";
cout<<"学生的人数是:"<<y<<endl;
cout<<"学生的总分是:"<<a<<endl;
cout<<"学生的平均分是:"<<z/y<<endl;
cout<<"学生的最高分是:"<<max<<endl;
cout<<"学生的最低分是:"<<min<<endl;
system("pause");
return 0;
}
3、计算学生成绩的优良率
#include<iostream>
using namespace std;
int main()
{
int y = 0, z = 0;
int b = 0, a = 0, c = 0, d = 0, e = 0, f = 0;
int x ;
cout << "请输入考生成绩";
cin >> x, cin.get();
while (true)
{
if(x == -1) break;
else
y=y+1; z = z + x; cin >> x, cin.get();
if (x == -1) break;
else if (x >= 90 && x <= 100) { a=a+1; continue; }
else if (x >= 80 && x <= 89) { c=c+1; continue; }
else if (x >= 70 && x <= 79) { d=d+1; continue; }
else if (x >= 60 && x <= 69) { e=e+1; continue; }
else if (x <= 59) { f=f+1; continue; }
}
cout << "优秀的人数为" << a << endl;
cout << "良好的人数为" << c << endl;
cout << "中等的人数为" << d << endl;
cout << "及格的人数为" << e << endl;
cout << "不及格的人数为" << f << endl;
cout << "平均分为:" << z / y;
system("pause");
return 0;
}
4、静态函数的使用示例
#include<iostream>
using namespace std;
class Student
{
private:
char*Name;
int No;
static int countS;
public:
static int GetCount()
{
return countS;
}
Student(char* = "", int = 0);
Student(Student &);
~Student();
};
Student::Student(char * Name, int No)
{
this->Name = new char[strlen(Name) + 1];
strcpy(this->Name, Name);
this->No = No;
++countS;
cout << "constructing:" << Name << endl;
}
Student::Student(Student&r)
{
Name = new char[strlen(r.Name) + 1];
strcpy(Name, Name);
No = r.No;
++countS;
cout << "copy constructing:" << r.Name << endl;
}
Student::~Student()
{
cout << "destructing:" << Name << endl;
delete[]Name;
--countS;
}
int Student::countS = 0;
int main()
{
cout << Student::GetCount() << endl; //使用类调用静态成员函数;
Student s1("Antony"); //建立一个新对象;
cout << s1.GetCount() << endl; //通过对象调用静态成员函数;
Student s2(s1); //利用已有对象建立一个新对象;
cout << s1.GetCount() << endl;
Student S3[2]; //建立一个对象数组;
cout << Student::GetCount() << endl;
Student*s4 = new Student[3]; //建立一个动态对象数组;
cout << Student::GetCount() << endl;
delete[]s4; //删除动态对象数组的内存空间;
cout << Student::GetCount() << endl;
return 0;
}
5、函数模板显式实例化
#include<iostream>
using namespace std;
template<class T1,class T2>
T1 add(T1 x, T2 y)
{
cout << "(" << sizeof(T1) << "," << sizeof(T2) << ")\t";
return x + y;
}
int main()
{
cout << add<int,int>(9,8)<< endl;
cout << add<double,double>(9.0, 8.0) << endl;
cout << add<int,double>(9, 8.0) << endl;
cout << add<double,int>(9.0, 8) << endl;
cout << add<char,char>('A', 'A' - '0') << endl;
cout << add<long int,int>(long(8), 9) << endl;
return 0;
}
6、函数模板隐式实例化
#include<iostream>
using namespace std;
template<class T1,class T2>
T1 add(T1 x, T2 y)
{
cout << "(" << sizeof(T1) << "," << sizeof(T2) << ")\t";
return x + y;
}
int main()
{
cout << add<int,int>(9,8)<< endl;
cout << add<double,double>(9.0, 8.0) << endl;
cout << add<int,double>(9, 8.0) << endl;
cout << add<double,int>(9.0, 8) << endl;
cout << add<char,char>('A', 'A' - '0') << endl;
cout << add<long int,int>(long(8), 9) << endl;
return 0;
}
7、后置++运算符重载
#include<iostream>
using namespace std;
class A
{
int x, y;
public:
A(int i, int j)
{
x = i;
y = j;
}
A operator ++(int)
{
return A(x++, y++);
}
~A()
{
}
void show()
{
cout << x << " " << y << endl;
}
};
int main()
{
A p(1, 2);
cout << "开始p的x和y的值为:" << endl;
p.show();
A c = p++;
cout << " ++重载后对象c的x和y的值为:" << endl;
c.show();
cout << "++重载后对象p的x和y的值为:" << endl;
p.show();
return 0;
}
8、揭开格式标志的秘密
#include<iostream>
using namespace std;
struct fmtflags
{
long flag;
char flagname[12];
}
flags[18] = { {ios::hex,"hex"},
{ios::dec,"dec"},
{ios::oct,"oct"},
{ios::basefield,"basefield"},
{ios::internal,"internal"},
{ios::left,"left"},
{ios::right,"right"},
{ios::adjustfield,"adjustfield"},
{ios::fixed,"fixed"},
{ios::scientific,"scientific"},
{ios::basefield,"basefield"},
{ios::showbase,"showbase"},
{ios::showpoint,"showpoint"},
{ios::showpos,"showpos"},
{ios::skipws,"skipws"},
{ios::uppercase,"uppercase"},
{ios::boolalpha,"boolalpha"},
{ios::unitbuf,"unitbuf"}
};
int main()
{
long IFlags;
IFlags = cout.setf(0, cout.flags());
cout.setf(ios::hex, ios::basefield);
cout << "Default flag is:" << IFlags << endl;
for (int i = 0; i < 18; i++)
cout << flags[i].flag << '\t' << flags[i].flagname << endl;
return 0;
}
9、虚函数的调用过程示例
#include<iostream>
using namespace std;
class A
{
public:
virtual void func1()
{
cout << "A1";
}
void func2()
{
cout << "A2";
}
};
class B :public A
{
public:
void func1()
{
cout << "B1";
}
void func2()
{
cout << "B2";
}
};
int main()
{
A* p = new B;
p->func1();
p->func2();
delete p;
return 0;
}
10、
定义动物Animal(属性:年龄:m_age行为:吃eat()和显示show()),由派Animal公有派生出食肉动物Carnivore(属性: 体重 m_weight 行为:吃肉 eat ()再由食肉动物Carnivore公有派生出猫cat(属性:颜色m_colour,行为:吃鱼 eat ()),各个类中自己实现构造函数和析构函数,在main函数中实现测试。
#include<iostream>
#include<string>
using namespace std;
class Animal //动物类
{
private:
int m_age; //年龄
public:
Animal(int age = 0)
{
m_age = age;
cout << "Animal构造完毕!" << endl;
}
~Animal()
{
cout << "Animal析构完毕!" << endl;
}
void eat()
{
cout << "吃!" << endl;
}
void show_Animal()
{
cout << "age:" << m_age << endl;
}
};
class Carnivore:public Animal //Carnivore类 公有继承Animal
{
private:
int m_weight; //体重
public:
Carnivore(int age, int weight) :Animal(age) //Carnivore 构造函数
{
m_weight = weight;
cout << "Carnivore构造完毕" << endl;
}
~Carnivore()
{
cout << "Carnivore析构完毕" << endl; //Carnivore 析构函数
}
void eat()
{
cout << "食肉动物" << endl;
}
void show_Carnivore()
{
Animal::show_Animal();
cout << "weight:" << m_weight << endl;
}
};
class Cat:public Carnivore //cat类公有继承食肉动物Carnivore
{
private:
string m_colour;
public:
Cat(int age,int weight,string n):Carnivore(age,weight) //cat 构造函数(参数个数取决于main函数中对象的参数,且类型个数11对应)
{
m_colour = n; //初始化m_colour
cout << "cat构造完毕!" << endl;
}
~Cat() //cat析构函数
{
cout << "cat 析构完毕" << endl;
}
void eat()
{
cout << "吃鱼" << endl;
}
void show_cat()
{
Carnivore::show_Carnivore();
cout << "colour:" << m_colour << endl;
}
};
int main()
{
Cat c1(1, 10, "白色");
c1.show_cat();
c1.eat();
return 0;
}
如有不足,希望大佬指正!