當前位置:首頁 » 編程軟體 » 類與對象編程題

類與對象編程題

發布時間: 2022-02-15 10:01:45

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;
}

熱點內容
內機額定容量配置率如何查 發布:2025-01-13 17:48:18 瀏覽:162
oicq伺服器地址 發布:2025-01-13 17:34:19 瀏覽:928
默認管理密碼是什麼 發布:2025-01-13 17:33:33 瀏覽:443
系列影視廣告文案腳本 發布:2025-01-13 17:31:57 瀏覽:792
防盜器編程 發布:2025-01-13 17:24:39 瀏覽:899
聯通電信伺服器怎麼不卡頓 發布:2025-01-13 17:21:30 瀏覽:821
科沃茲低配可以升級哪些配置 發布:2025-01-13 17:09:26 瀏覽:330
android判斷資料庫是否存在 發布:2025-01-13 17:08:17 瀏覽:334
ie腳本運行錯誤 發布:2025-01-13 17:08:05 瀏覽:623
python中或者怎麼表示 發布:2025-01-13 16:32:33 瀏覽:291