您的位置:首页 > 编程语言 > C语言/C++

C/C++入门基础 之类方法 入门篇

2015-07-25 10:15 344 查看
都是初学时,自己写的代码,

有些是某些书上的习题,有些是例题,

总之这就是一个合集,本次不一定可以连载完,会分为几个系列

#include<iostream>

#include<cmath>

using namespace std;

class Rectangle{

private:
double left,top;
double right,bottom;

public:
Rectangle(double l=0,double t=0,double r=0,double b=0);
~Rectangle(){};
void Assign(double l,double t,double r,double b);
void Show();
double Area();
double Perimeter();

};

Rectangle::Rectangle(double l,double t,double r,double b)http://202.114.240.111/eportal/pcLogin_0269f68528ce49769904ed56d4c63655.html?fromHtml=true&version=52472d65506f7274616c20454e54455250524953455f312e3433287032295f4275696c643230313430333036&userAgentForLogin=0&wlanuserip=10.3.18.191&wlanacname=Ruijie_Ac_800a2e&ssid=WUST_Wireless&nasip=10.100.0.154&mac=645a0476997c&t=wireless-v2-plain&url=http://www.baidu.com/baidu?wd=123&tn=monline_4_dg&pageUuid=0269f68528ce49769904ed56d4c63655

{
left=l;
top=t;
right=r;
bottom=b;

}

void Rectangle::Assign(double l,double t,double r,double b)

{
left=l;
top=t;
right=r;
bottom=b;

}

void Rectangle::Show()

{
cout<<"left-top point is("<<left<<","<<top<<")"<<endl;
cout<<"right-bottom point is("<<right<<","<<bottom<<")"<<endl;

}

double Rectangle::Area()

{
return fabs((right-left)*(bottom-top));

}

double Rectangle::Perimeter()

{
return 2*(fabs(right-left)*fabs(bottom-top));

}

int main()

{
Rectangle rect;
rect.Show();
rect.Assign(100,200,300,400);
rect.Show();

Rectangle rect1(0,0,200,200);
rect.Show();
cout<<"面积"<<rect.Area()<<'\t'<<"周长"<<rect.Perimeter()<<endl;
cout<<"面积"<<rect1.Area()<<'\t'<<"周长"<<rect1.Perimeter()<<endl;
return 0;

}

#include<iostream>

using namespace  std;

const double pi=3.1415926;

class Circle{

public:
Circle(double r1=0);
Cricle(Circle &n);

    ~Circle(){};
double getarea(){  return area;  }
double getcircumference(){  return circumference;  }

private:
double r,area ,circumference;

};

 

Circle::Circle(double r1):r(r1)

{
area=r*r*pi;
circumference=2*r*pi;

}

Circle::Cricle(Circle &n)

{
r=n.r;
area=n.area;
circumference=n.circumference;

}

void main()

{

    Circle h(10);
cout<<h.getarea()<<" "<<h.getcircumference()<<endl;

}

#include<iostream>

using namespace std;

class  Time

{

private:
int s,h,m;

public:
Time(int H=0,int M=0,int S=0 );
Time(Time &p);
void show();

};

void Time::show()

{
cout<<h<<":"<<m<<":"<<s<<endl;

}

Time::Time(int H,int M,int S )
{
h=H;
s=S;
m=M;
}

Time::Time(Time &p)
{
h=p.h;
s=p.s;
m=p.m;
}

class Date 

{

private:
int year,month,day;
Time time;

public:
Date(Time &q,int y=0,int m=0,int d=0);
Date(Date &a);
void show();

};

Date::Date(Time &q,int y,int m,int d):time(q)
{
year=y;
month=m;
day=d;
}

Date::Date(Date &a):time(a.time)
{
year=a.year;
day=a.day;
month=a.month;
}

void Date::show()

{
time.show();
cout<<year<<"--"<<month<<"--"<<day<<endl;

}

void main()

{

    Time time(12,11,11);
time.show();
Date date(time,2014,1,1);
date.show();

}

#include<iostream>

using namespace std;

class Date

{

private:
int year,month,day;

public:
Date(int y=0,int m=0,int d=0);
Date(Date &q);
~Date(){}
void show();

};

Date::Date(int y,int m,int d)

{
year=y;
month=m;
day=d;

}

Date::Date(Date &q)

{
year=q.year;
month=q.month;
day=q.day;

}

void Date::show()

{
cout<<year<<"--"<<month<<"--"<<day<<
4000
endl;

}

class Personnel

{

private:

    char num[20],sex[10],name[20],id[20];
Date birthday;

public:
Personnel(){}
Personnel(Date &q,char nu[20],char se[10],char na[20],char ID[20]);
Personnel(Personnel &a);
~Personnel(){}
void show();

};

Personnel::Personnel(Date &q,char nu[20],char se[10],char na[20],char ID[20]):birthday(q)

{
strcpy(sex,se);
strcpy( name,na);

    strcpy(id,ID);
strcpy(num,nu);

}

Personnel::Personnel(Personnel &a):birthday(a.birthday)

{
strcpy(sex,a.sex);
strcpy( name,a.name);

    strcpy(id,a.id);
strcpy(num,a.num);

}

void Personnel::show()

{
birthday.show();
cout<<"name:"<<name<<" sex:"<<sex<<" number:"<<num<<" ID:"<<id<<endl;

}

void main()

{
Date d(2014,1,1);
Personnel p(d,"2013131313","ÄÐ","СÀî","342400000");
p.show();

}

#include<iostream>

using namespace std;

class Client

{

private:
static char severname[20];
static int clientnum;

public:

Client(){
clientnum++; }
~Client(){
clientnum--; }
static void Changesevername(char *a)
{
strcpy(severname,a);
}
static int getclientnum(){  return clientnum;  }
static char* getsevername(){ return  severname ;  }

};

int Client::clientnum=0;

char Client::severname[20];

void main()

{

   cout<< Client::getclientnum()<<endl;

   Client::Changesevername("xxxxxxx");

   cout<<"建立对象"<<endl;

   Client t;

   cout<<t.getclientnum()<<endl;

   cout<<t.getsevername()<<endl;

   cout<<"建立对象"<<endl;

   Client t1;

   cout<<t.getclientnum()<<endl;

   cout<<t.getsevername()<<endl;

}

#include<iostream>

using namespace std;

class Student

{

private:

    char name[20],studentno[20],sex[5];
static int studentnum;

public:
Student(char* na,char* studen,char* se)
{

        strcpy(name,na);
strcpy(studentno,studen);
strcpy(sex,se);
studentnum++;
}
~Student()
{
studentnum--;
}
void set(char* na,char* studen,char* se)
{

        strcpy(name,na);
strcpy(studentno,studen);
strcpy(sex,se);

}
static int getstudentnum()
{
return studentnum;
}
static void show(Student &a)
{
cout<<"StudentNo: "<<a.studentno<<" name:"<<a.name<<" sex:"<<a.sex<<endl;
}

};

int Student::studentnum=0;

void main()

{
cout<<"创建对象数:"<<endl;
cout<<Student::getstudentnum()<<endl;
Student s("小李","20131313000","男");
s.show(s);
cout<<"创建对象数:"<<endl;
cout<<Student::getstudentnum()<<endl;
{
Student s1("小王","20141414000","女");
s1.show(s1);
cout<<"创建对象数:"<<endl;
cout<<Student::getstudentnum()<<endl;
}
Student s2("小真","20151515000","男");
s2.show(s2);
cout<<"创建对象数:"<<endl;
cout<<Student::getstudentnum()<<endl;

};

#include<iostream>

#include<string>

using namespace std;

class Student

{

private:
string studentno,name;
float score;

public:
Student(){}
Student(string studentno,string name,float score);
Student(Student &a);
~Student(){}
void changer(string studentno,string name,float score);
void show();
friend void show(double h, int i,Student *p);
friend float search(Student *p,int i);

};

Student::Student(string studentno,string name,float score)

{
this->studentno=studentno;
this->name=name;
this->score=score;

}

void Student::changer(string studentno,string name,float score)

{
this->studentno=studentno;
this->name=name;
this->score=score;

}

Student::Student(Student &a)

{

    studentno=a.studentno;
name=a.name;
score=a.score;

}

void Student::show()

{
cout<<"StudentNO:"<<studentno<<" name:"<<name<<" score:"<<score<<endl;

}

float search(Student *p,int i)

{

   double high=p[0].score;

   for(int j=0;j<i;j++)
  if(high<p[j].score)
  high=p[j].score;

     return high;

}

void show(double high, int i ,Student *p)

{
for(int j=0;j<i;j++)
   if(high==p[j].score)
p[j].show();

}

int main()

{

   cout<<"输入学生人数: ";

   int i;

   cin>>i;

   string studentno,name;

   float score;

   Student *p=new Student[i];

   cout<<"请输入学生 学号,姓名,分数"<<endl;

   for(int j=0;j<i;j++)

   {
  cin>>studentno>>name>>score;

          p[j].changer( studentno, name, score);

   }

   double h=search(p,i);

   show(h,i,p);

   return 0;

}

#include<iostream>

#include<string>

using namespace std;

class Date

{

private:
int year,month,day;

public:
Date(){}
Date(int year,int month,int day)
{
this->year=year;
this->month=month;
this->day=day;
}
Date(Date &a)
{
year=a.year;
month=a.month;
day=a.day;
}
void set(int year,int month,int day)
{

      this->year=year;
this->month=month;
this->day=day;
}
void show()
{
cout<<year<<"--"<<month<<"--"<<day<<endl;
}

};

class People

{

private:
string number,sex,id;
char *name;
Date birthday;

public:
People(){
  name=new char[20];
}
People( Date &a,char *name,string number,string sex,string id):birthday(a)
{

          this->name=new char[strlen(name)+1];
 strcpy(this->name,name);
 this->number=number;
 this->sex=sex;
 this->id=id;
}

People(People &a):birthday(a.birthday)
{
name=a.name;
number=a.number;
sex=a.sex;
id=a.id;
}

void show()
{
cout<<name<<ends<<number<<ends<<sex<<ends<<id<<endl;
birthday.show();
}
void set( int year,int month,int day,char *name,string number,string sex,string id)
{
 birthday.set(year,month, day);
 this->name=new char[strlen(name)+1];
     strcpy(this->name,name);
 this->number=number;
 this->sex=sex;
 this->id=id;
}

};

void main()

{

    string number,sex,id;
char name[30];
int year,month,day;

    int i,j;
cout<<"请输入人员个数"<<endl;

    cin>>i;

    People *p=new People[i];
cout<<"人员:姓名,学号,性别,id,出生年月日"<<endl;
for(j=0;j<i;j++)
{
cin>>name>>number>>sex>>id;
cin>>year>>month>>day;
p[j].set( year, month, day,name,number, sex, id);
}
for(j=0;j<i;j++)
{
p[j].show();
}

}

#include<iostream>

#include<cmath>

const double pi=3.1415926;

using namespace std;

class Point

{

protected:
double x,y;

public:

Point (){}
Point(double x,double y)
{
this->x=x;
this->y=y;
}

Point(Point &a)
{
x=a.x;
y=a.y ;
}
~Point(){}
void Show();
virtual double Area(){return 0;}
virtual double length(){return 0;}

};

void Point::Show()

{
cout<<"("<<x<<","<<y<<")"<<endl;

}

class Circle:public Point

{

protected:
double r;

public:
Circle(){}
virtual ~Circle(){}
Circle(double xv,double yv,double rv):Point(xv,yv)
{
r=rv;
}
Circle(Circle &a);
double Area();
void Show();
double length(){return 0;}

};

Circle::Circle(Circle &a)

{
x=a.x;
y=a.y;
r=a.r;

}

double Circle::Area()

{
return (pi*r*r);

}

void Circle::Show()

{
Point::Show();
cout<<r<<endl;

}

class Cylinder:public Circle

{

private:
double h;

public:
Cylinder(){}
virtual ~Cylinder(){}
Cylinder(Cylinder &a);
Cylinder(double xv,double yv,double rv,double hv);
void Show();
double Area();
double length(){return 0;}

};

Cylinder::Cylinder(Cylinder &a)

{
x=a.x;
y=a.y;
r=a.r;
h=a.h;

}

Cylinder::Cylinder(double xv,double yv,double rv,double hv):Circle(xv,yv,rv)

{
h=hv;

}

 void Cylinder::Show()

 {
Circle::Show();
cout<<h<<endl;

 }

 double Cylinder::Area()

 {
return (Circle::Area()*2+2*pi*r*h);

 }

int main()

{
Circle c(1,2,3);
c.Show();
cout<<c.Area()<<endl;
Cylinder y(1,2,3,4);
y.Show();
cout<<y.Area()<<endl;

    return 0;

}

#include<iostream>

#include<cmath>

using namespace std;

class Point

{

protected:
double x,y;

public:

Point (){}
Point(double x,double y)
{
this->x=x;
this->y=y;
}
Point(Point &a)
{
x=a.x;
y=a.y ;
}
~Point(){}
void Show();

};

class Rectangle:public Point

{

protected:
double lon,wide;

    Point p;

public:
Rectangle(){}
Rectangle(Point p1,double l,double w);
Rectangle(Rectangle &a);
~Rectangle(){}
void Show();

};

class Cube:public Rectangle

{

private:
double high;

public:
Cube(double l,double w,double h,Point p1);
Cube(Cube &a);
~Cube(){}
void Show();
double area();
double volume();

};

void Point::Show()

{
cout<<"("<<x<<","<<y<<")"<<endl;

}

Rectangle::Rectangle(Point p1,double l,double w):p(p1)
{
lon=l;
wide=w;
}

void Rectangle::Show()

{
p.Show ();
cout<<lon<<"  "<<wide<<endl;

}

Rectangle::Rectangle(Rectangle &a):Point(a)

{

  lon=a.lon;

  wide=a.wide;

}

Cube::Cube(double l,double w,double h,Point p1):Rectangle(p1,l,w)
{
high=h;
}

Cube::Cube(Cube &a):Rectangle(a)

{
high=a.high;

}

double Cube::area()

{
return 2*(lon*wide+lon*high+wide*high);

}

double Cube::volume()

{
return lon*wide*high;

}

void Cube::Show()

{

Rectangle::Show();
cout<<high<<endl;

}

void main()

{

    Point x(1,2),y(3,4);
Rectangle t(x,1,2);
t.Show();
Cube h(1,2,3,x);
h.Show();

    cout<<h.area()<<ends<<h.volume()<<endl;

}

#include<iostream>

#include<string>

using namespace std;

class People

{

protected:
string name,sex,tel;
int age;

public:

    People(string name,string sex,int age,string tel);
People(){}
People(People &a);
void Show();

};

People::People(string name,string sex,int age,string tel)

{
this->name=name;
this->sex=sex;
this->age=age;
this->tel=tel;

}

void People::Show()

{
cout<<"name: "<<name<<" sex: "<<sex<<" age: "<<age<<" tel: "<<tel<<endl;

}

People::People(People &a)

{
name=a.name;
age=a.age;
sex=a.sex;
tel=a.tel;

}

class Teacher:virtual public People

{

protected:
string tepost;

public:
Teacher(){}
Teacher(People &a,string tepost);
Teacher(Teacher &a);
void Show();

};

Teacher::Teacher(People &a,string tepost):People(a)

{
this->tepost=tepost;

}

Teacher::Teacher(Teacher &a):People(a)

{
tepost=a.tepost;

}

void Teacher::Show()

{
People::Show();
cout<<"the title of a technical post: "<<tepost<<endl;

}

class cadre:virtual public People

{

protected:
string post;

public:
cadre(){}
cadre(People &a,string post);
cadre(cadre &a);
void Show();

};

cadre::cadre(People &a,string post):People(a)                                                                                  

{
this->post=post;

}

cadre::cadre(cadre &a):People(a)

{
post=a.post;

}

void cadre::Show()

{
People::Show();
cout<<" post: "<<post<<endl;

}

class Double:public Teacher,public cadre

{

private:
int wage;

public:
Double(){};
Double(Teacher &a, cadre &b,int wage);
Double(Double &a);
void Show();

};

Double::Double(Teacher &a, cadre &b,int wage):People(a),Teacher(a),cadre(b)

{
this->wage=wage;

}

Double::Double(Double &a):Teacher(a),cadre(a),People(a)

{
wage=a.wage;

}

void Double::Show()

{

People::Show();
cout<<" post: "<<post<<"  the title of a technical post:  "<<tepost<<" wage: "<<wage<<endl;

}

void main()

{
People a("小李","男",11,"15671641760");

// a.Show();

// cout<<endl;
Teacher b(a,"中级");
b.Show();
cout<<endl;
cadre c(a,"hj");

// c.Show();

// cout<<endl;
Double d(b,c,20000);
d.Show();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CC++ 源码