using namespace std;
class Base1{
public:
Base1(int i){cout<<"Constructing Base1"<<i<<endl;}
};
class Base2{
public:
Base2(int j){cout<<"Constucting Base2"<<j<<endl;}
};
class Base3{
public:
Base3(){cout<<"Constructing Base3*"<<endl;}
};
class Derived:public Base2,public Base1,public Base3{
public:
Derived(int a,int b,int c,int d):Base1(a),member2(d),member1(c),Base2(b)
{}
private:
Base1 member1;
Base2 member2;
Base3 member3;
};
int main(){
Derived obj(1,2,3,4);
return 0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include <iostream>
using namespace std;
enum GameResult{WIN,LOSE,TIE,CANCEL};
int main(){
GameResult result;
enum GameResult omit=CANCEL;
for(int count=WIN;count<=CANCEL;count++){
result=GameResult(count);
if(result==omit)
cout<<"The game was cancelled"<<endl;
else{
cout<<"The game was played";
if(result==WIN)
cout<<"and we won!";
if(result=LOSE)
cout<<"and we lost.";
cout<<endl;
}
}
return 0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include <iostream>
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){
x=xx;
y=yy;
}
Point(Point &p);
int getX(){
return x;
}
int getY(){
return y;
}
private:
int x,y;
};
Point::Point(Point &p){
x=p.x;
y=p.y;
cout<<"Calling the copy constructor"<<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;
return 0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include <iostream>
using namespace std;
const float PI=3.141593;
const float FENCE_PRICE=35;
const float CONCRETE_PRICE=20;
class Circle{
public:
Circle(float r);
float circumference();
float area();
private:
float radius;
};
Circle::Circle(float r){
radius=r;
}
float Circle::circumference(){
return 2*PI*radius;
}
float Circle::area(){
return PI*radius*radius;
}
int main()
{
float radius;
cout<<"Enter the radius of the pool:";
cin>>radius;
Circle pool(radius);
Circle poolRim(radius+3);
float fenceCost=poolRim.circumference()*FENCE_PRICE;
cout<<"Fencing Cost is $"<<fenceCost<<endl;
float concreteCost=(poolRim.area()-pool.area())*CONCRETE_PRICE;
cout<<"Concrete Cost is $"<<concreteCost<<endl;
return 0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include <iostream>
#include<cmath>
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){
x=xx;
y=yy;
}
Point(Point &p);
int getX(){return x;}
int getY(){return y;}
private:
int x,y;
};
Point::Point(Point &p){
x=p.x;
y=p.y;
cout<<"calling the copy constructor of Point"<<endl;
}
class Line{
public:
Line(Point xp1,Point xp2);
Line(Line &l);
double getLen(){return len;}
private:
Point p1,p2;
double len;
};
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2){
cout<<"Calling constructor of Line"<<endl;
double x=static_cast<double>(p1.getX()-p2.getX());
double y=static_cast<double>(p1.getY()-p2.getY());
len=sqrt(x*x+y*y);
}
Line::Line(Line &l):p1(l.p1),p2(l.p2){
cout<<"Calling the copy constructor of Line"<<endl;
len=l.len;
}
int main(){
Point myp1(1,1),myp2(4,5);
Line line(myp1,myp2);
Line line2(line);
cout<<"The length of the line2 is:";
cout<<line2.getLen()<<endl;
return 0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include<string>
#include <iostream>
using namespace std;
inline void test(const char*title,bool value){
cout<<title<<"returns"<<(value?"true":"false")<<endl;
}
int main(){
string s1="DEF";
cout<<"s1 is"<<s1<<endl;
string s2;
cout<<"please enter s2:";
cin>>s2;
cout<<"length of s2:"<<s2.length()<<endl;
test("s1<=\"ABC\"",s1<="ABC");
test("\"DEF\"<=s1","DEF"<=s1);
s2+=s1;
cout<<"s2=s2+s1:"<<s2<<endl;
cout<<"length of s2:"<<s2.length()<<endl;
return 0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include <iostream>
#include<string>
using namespace std;
int main(){
for(int i=0;i<2;i++){
string city,state;
getline(cin,city,',');
getline(cin,state);
cout<<"City:"<<city<<"State:"<<state<<endl;
}
return 0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include <iostream>
using namespace std;
class Base1{
public:
void display()const{cout<<"Base1::display()"<<endl;}
};
class Base2:public Base1{
public:
void display()const{cout<<"Base2::display()"<<endl;}
};
class Derived:public Base2{
public:
void display()const{cout<<"Derived::display()"<<endl;}
};
void fun(Base1*ptr){
ptr->display();
}
int main(){
Base1 base1;
Base2 base2;
Derived derived;
fun(&base1);
fun(&base2);
fun(&derived);
return 0;
}