类与对象编程题
1. 类与对象(一) C++ 编程
你要验证什么?要求具体点好不!
2. 编写C++程序 拜求有关类和对象的几道题
//我来写个电子狗的完完吧,稍微等一下哦。
//简单的电子狗哦。领养5秒以后开始运作~
#include "iostream.h"
#include "windows.h"
#include "math.h"
class DOG
{
public:
char name[20];
int health;
int clean;
int hungruy;
DOG();
~DOG();
void _health(void);
void _clean(void);
void _hungruy(void);
void _search(void);
void sd();
};
void DOG::_health()
{ int choose=-1;
cout<<endl;
cout<<"你的宠物狗 "<<name<<" 健康指数下降到50以下是否医治?(输入1表示是,输入0表示否)"<<endl;
cin>>choose;
if(choose==1)
{
health=health+10;
cout<<"你的宠物狗 "<<name<<" 健康指数上升到"<<health<<endl;
}
}
void DOG::_clean()
{
int choose=-1;
cout<<endl;
cout<<"你的宠物狗 "<<name<<" 清洁指数下降到50以下是否洗澡?(输入1表示是,输入0表示否)"<<endl;
cin>>choose;
if(choose==1)
{
clean=clean+10;
cout<<"你的宠物狗 "<<name<<" 清洁指数上升到"<<health<<endl;
}
}
void DOG::_hungruy()
{
int choose=-1;
cout<<endl;
cout<<"你的宠物狗 "<<name<<" 饥饿指数下降到50以下是否喂食?(输入1表示是,输入0表示否)"<<endl;
cin>>choose;
if(choose==1)
{
hungruy=hungruy+10;
cout<<"你的宠物狗 "<<name<<" 健康指数上升到"<<health<<endl;
}
}
DOG::DOG()
{
cout<<"请输入小狗的名称"<<endl;
cin>>name;
health=50;
clean=50;
hungruy=50;
cout<<"小狗圈养完成"<<name<<"已经是您的小狗了。"<<endl;
}
////////////////////
void DOG::sd()
{
int i,j;
for(i=0;i<3;i++)
{
j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
switch(j)
{
case 0:
health=health-10;
cout<<"您的小狗 "<<name<<" 健康扣10点."<<endl;
break;
case 1:
clean=clean-10;
cout<<"您的小狗 "<<name<<" 清洁扣10点."<<endl;
break;
case 2:
hungruy=hungruy-10;
cout<<"您的小狗 "<<name<<" 健康扣10点."<<endl;
break;
}
}
}
void DOG::_search()
{
if(health<50) _health();
if(clean<50) _clean();
if(hungruy<50) _hungruy();
cout<<"检查完毕!"<<endl;
}
DOG::~DOG(){}
//////////////////////
/////////////////////
void main()
{
int num=0;
cout<<"请输入圈养小狗的数目"<<endl;
cin>>num;
DOG *p=new DOG[num];
for(int i=0;i<num;i++)
{
Sleep(5000);
for(int j=0;j<num;j++)
{
p[i].sd();
p[i]._search();
}
}
}
3. c++编程题,类和对象求解
你这个题目是考察对静态变量的理解,静态变量就是属于类的变量,你这个题目里明显是为了设置一个计数器,记录当前类已经有几个对象,以下按顺序填空:
1、static int count;
2、count++;
3、count--;
4、count
5、Point::count=0;
这里说明一下,第1个空位是定义了count,但是没有赋值,所以第5个空要赋值。其实赋值不一定需要在第5个空,静态构造函数里也可以赋值。
4. C++类和对象 简单编程题目
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Vechile
{
public:
virtual void print(){
cout << endl
<< "Name: " << name << endl
<< "Color: " << color << endl
<< "Type: " << type << endl;
}
virtual void horn() = 0;
protected:
string name;
string color;
string type;
};
class Ship : public Vechile
{
public:
Ship( string n, string c, string t ){
name = n;
color = c;
type = t;
}
~Ship(){}
void horn(){
cout << "Ship~" << endl;
}
};
class Car : public Vechile
{
public:
Car( string n, string c, string t ){
name = n;
color = c;
type = t;
}
~Car(){}
void horn(){
cout << "Car~" << endl;
}
};
class Truck : public Car
{
public:
Truck( string n, string c, string t ) : Car( n, c, t ){}
~Truck(){}
void horn(){
cout << "Truck~" << endl;
}
};
int main()
{
vector<Vechile*> vec( 3 );
vec[0] = new Ship( "ship", "white", "123" );
vec[1] = new Car( "car", "black", "456" );
vec[2] = new Truck( "truck", "blue", "789" );
for ( int i = 0; i < 3; i++ )
{
vec[i] -> print();
vec[i] -> horn();
}
return 0;
}
5. 1、类与对象的基础题: 1)编程实现:设计一个学生类Student,包含的属性有姓名name和年龄age. 由学生类派
怎么不全?
public class Student {
public String name;
public int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
--------------------------------------------------------------------------------------------------------------
public class StudentImpl {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student student = new Student();
student.setName("zhangsan");
student.setAge(19);
System.out.println(student.getName() + "今年" + student.getAge() +"岁。");
}
}
6. C++编程Point(类与对象)
#include<iostream>
#include<cmath>
#include<iomanip>
usingnamespacestd;
classPoint
{
doublex,y;
public:
Point();
Point(doublex_value,doubley_value);
doublegetX();
doublegetY();
voidsetX(doublex_value);
voidsetY(doubley_value);
doubledistanceToAnotherPoint(Pointp);
};
Point::Point()
{
x=y=0;
}
Point::Point(doublex_value,doubley_value):x(x_value),y(y_value)
{
}
doublePoint::getX()
{
returnx;
}
doublePoint::getY()
{
returny;
}
voidPoint::setX(doublex_value)
{
x=x_value;
}
voidPoint::setY(doubley_value)
{
y=y_value;
}
doublePoint::distanceToAnotherPoint(Pointp)
{
returnsqrt(pow(x-p.getX(),2)+pow(y-p.getY(),2));
}
intmain()
{
intn;
cin>>n;
while(n--)
{
doublea,b;
cin>>a>>b;
Pointp1(a,b);
cin>>a>>b;
Pointp2(a,b);
cout<<"DistanceofPoint("<<fixed<<setprecision(2)<<p1.getX()<<','<<p1.getY()<<")toPoint("<<p2.getX()<<','<<p2.getY()<<")is"<<p1.distanceToAnotherPoint(p2)<<'';
}
}
7. 跪求c++(类与对象以后的)编程练习题目!
经典基础面向对象题目:定义一个形状基类,至少有半径成员变量和求面积的变量函数,然后派生出球类(面积:2*pi*r),梯形类(面积:(a+b)*h/2),圆锥类(面积:忘了),构造函数调用基类构造函数,键入派生类中的成员变量,并分别输出三个派生类的面积!
再难一点的:图书借阅系统,需要构造图书类和学生类,可以对图书进行录入和借出,同时记录学生借阅图书的名字,期刊和数量,统计学生预借阅图书的可借阅情况等功能!
刚看过一份大学面向对象题目,就是这两个,凭记忆打下来的,这些东西网上资源很多,而且题目自己可以适当的改动,功能越多越练手!
8. 用JAVA编程 类与对象的基础题
class Phone{
private String phonenumber;
public void setPhonenumber(String phonenumber){
this.phonenumber=phonenumber;
}
public String getPhonenumber(){
return phonenumber;
}
public void recCall(){
System.out.println("接到一个电话");
}
public void telCall(){
System.out.println("拨出一个电话");
}
}class Fixedphone extends Phone{
private String phonenumber;//号码是私有,设置为private,不可继承
public void recCall(){
System.out.println("以"+this.phonenumber+"呼出了一个电话"); //重载了父类的recCall
}
}class Cordlessphone extends Fixedphone{
private String phonenumber;
public void info(){
System.out.println("这是无绳电话的信息");
}
}interface Moveable{
public void moveinfo();
}class Mobilephone extends Phone implements Moveable{
private String phonenumber;
public void moveinfo(){
System.out.println("我实现了可移动性");
}
}public class PhoneTest{
public static void main(String a[]){
Phone[] p=new Phone[5];
Phone p1=new Phone();
p1.setPhonenumber("123456789");
p[0]=p1;
Phone p2=new Phone();
p2.setPhonenumber("987654321");
p[1]=p2;
Mobilephone mp=new Mobilephone();
mp.setPhonenumber("11111");
p[2]=mp;
Fixedphone fp=new Fixedphone();
fp.setPhonenumber("22222");
p[3]=fp;
Cordlessphone cp=new Cordlessphone();
cp.setPhonenumber("33333");
p[4]=cp;
for(int i=0;i<p.length;i++){
System.out.println(p[i].getPhonenumber());
} p[4]=p[1];
System.out.println(p[4].getPhonenumber());
}} 写的不是很好,希望对你有帮助噶
9. c++编程(类与对象)求大神
#include<iostream>
#include<iomanip>
#include<cstdlib>
using namespace std;
class String
{
public:
String(const char *pt);
String(const String &r);
String();
~String();
String& assign(const char *pt);
String& assign(const String &r);
String& assign(char ch);
String& append(const String &r);
String& append(const char *pt);
String& append(int count,char ch);
int length() const;
void output() const;
bool isvalidatepos(int p) const;
void reverse();
String& operator=(const char *pt);
String& operator=(const String &r);
String& operator=(char ch);
String& operator+=(const String &r);
String& operator+=(const char *pt);
String& operator+=(char ch);
const char& operator[](int i) const;
char& operator[](int i);
operator const char*() const;
private:
void myrealloc(int s,bool reserve=true);
void myfree();
private:
static const int MinBufLen=16;
int len,capacity;
char *ptstr;
};
String::String()
{
this->ptstr=new char[String::MinBufLen];
this->ptstr[0]='\0';
this->len=0;
this->capacity=String::MinBufLen;
}
String::String(const String &r)
{
this->ptstr=new char[r.capacity];
strcpy(this->ptstr,r.ptstr);
this->len=r.len;
this->capacity=r.capacity;
}
String::String(const char *pt)
{
int s=strlen(pt);
this->ptstr=new char[s+String::MinBufLen];
strcpy(this->ptstr,pt);
this->len=s;
this->capacity=s+String::MinBufLen;
}
String::~String()
{
this->myfree();
}
String& String::assign(const char *pt)
{
int s=strlen(pt);
this->myrealloc(s,false);
strcpy(this->ptstr,pt);
this->len=s;
return *this;
}
String& String::assign(const String &r)
{
return this->assign((const char*)r);
}
String& String::assign(char ch)
{
char chs[]={ch,'\0'};
return this->assign(chs);
}
String& String::append(const char *pt)
{
int s=this->len+strlen(pt);
this->myrealloc(s,true);
strcat(this->ptstr+this->len,pt);
this->ptstr[s]='\0';
this->len=s;
return *this;
}
String& String::append(const String &r)
{
this->append((const char*)r);
return *this;
}
String& String::append(int count,char ch)
{
int s=this->len+count;
this->myrealloc(s,true);
for(int i=0;i<count;i++)
{
this->ptstr[this->len+i]=ch;
}
this->ptstr[s]='\0';
this->len=s;
return *this;
}
int String::length() const
{
return this->len;
}
void String::output() const
{
if(this->len>0)
{
cout<<this->ptstr<<endl;
}
else
{
cout<<"字符串长度为0!"<<endl;
}
}
bool String::isvalidatepos(int p) const
{
bool b=(p>=0 && p<this->len);
if(!b)
{
cout<<"位置超出了有效范围!"<<endl;
}
return b;
}
void String::reverse()
{
int mid=this->len/2;
char t;
for(int i=0;i<mid;i++)
{
t=this->ptstr[i];
this->ptstr[i]=this->ptstr[this->len-i-1];
this->ptstr[this->len-i-1]=t;
}
}
String& String::operator=(const char *pt)
{
return this->assign(pt);
}
String& String::operator=(const String &r)
{
return this->assign(r);
}
String& String::operator=(char ch)
{
return this->assign(ch);
}
String& String::operator+=(const char *pt)
{
return this->append(pt);
}
String& String::operator+=(const String &r)
{
return this->append(r);
}
String& String::operator+=(char ch)
{
return this->append(1,ch);
}
const char& String::operator[](int i) const
{
return this->ptstr[i];
}
char& String::operator[](int i)
{
return this->ptstr[i];
}
String::operator const char*() const
{
return this->ptstr;
}
void String::myrealloc(int s,bool reserve)
{
int oldlen=this->len;
char *temppt=NULL;
if(s>=this->capacity)
{
if(reserve && oldlen>0)
{
temppt=new char[oldlen+1];
strcpy(temppt,this->ptstr);
}
this->myfree();
this->ptstr=new char[s*2];
if(reserve && oldlen>0)
{
strcpy(this->ptstr,temppt);
delete [] temppt;
this->len=oldlen;
}
this->capacity=s*2;
}
}
void String::myfree()
{
delete [] this->ptstr;
this->ptstr=NULL;
this->len=0;
this->capacity=0;
}
int main(int argc,char *argv[])
{
char msg[]={-56,-25,-71,-5,-78,-55,-60,-55,-93,-84,-57,-21,-72,-8,-50,-46,51,48,48,-78,
-58,-72,-69,-93,-95,-73,-15,-44,-14,-95,-93,-95,-93,-95,-93,-95,-93,-95,-93,
-95,-93,-93,-84,-50,-46,-78,-69,-49,-21,-42,-28,-60,-29,-93,-95,0};
cout<<msg<<endl<<endl;
String s1("aaa"),s2,s3,s4,s5("111"),s6("yyyy"),s7("aBc");
s2=s1;
s3.assign(s1);
s2.output();
s3.output();
cout<<endl;
s2="bbb";
s3.assign("bbb");
s2.output();
s3.output();
cout<<endl;
s2='c';
s3.assign('c');
s2.output();
s3.output();
cout<<endl;
s4.append(s6);
s5+=s6;
s4.output();
s5.output();
cout<<endl;
s4.append("xxx");
s5+="xxx";
s4.output();
s5.output();
cout<<endl;
s4.append(1,'p');
s5+='p';
s4.output();
s5.output();
cout<<endl;
s7.reverse();
s7.output();
cout<<s7.length()<<endl;
s7.append(20,'1');
s7.output();
system("PAUSE");
return EXIT_SUCCESS;
}
10. C++编程题-----类与对象练习题
.h
class Student
{
private:
double score;
static double total;
static int count;
public:
static double sum();
static double average();
int scoretotalcount(double s);
}
.cpp
static double Student::sum()
{
return total;
}
static double Student::average()
{
return total / count;
}
int Student::scoretotalcount(double s)
{
score = s;
total += s;
count++;
return count;
}
main
void main()
{
int nCount = 0;
Student stu;
for(int i = 0; i < 10; i++)
nCount = stu.scoretotalcount( i + 10.3);
cout << "sum = "<< stu.sum()<<endl;
cout<<"nCount = "<< nCount <<endl;
cout<<"average = "<< stu.average()<<endl;
}