声明一个简单的函数Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等,有两个公有成员函
class Computer{
private:
Cpu cpu;
Ram ram;
Cdrom cdrom;
public:
void run()
{}
void stop()
{}
}
#include
#include
using namespace std;
class Computer
{
private:
string cpu;
int ram;
int cdrom;
public:
Computer(string CPU,int Ram,int Cdrom)
{
cpu=CPU;
ram=Ram;
cdrom=Cdrom;
}
string Getcpu() {return cpu;}
int Getram() {return ram;}
int Getcdrom() {return cdrom;}
};
int main()
{
string a;
int b;
int c;
cout<<"CPU:";
cin>>a;
cout<<"Ram:";
cin>>b;
cout<<"CDrom";
cin>>c;
Computer mypc(a,b,c);
cout<<"CPU:"<<mypc.Getcpu()<<"
"<<"Ram:"<<mypc.Getram()<<"
"<<"CDrom"<<mypc.Getcdrom()<<endl;
}
构造了一个Computer类,并且可以实现输入你自己电脑的CPU,RAM,CDRAM并把它们显示出来的功能。
enum CPU_rank{p1=1,p2,p3,p4,p5,p6,p7};
class CPU
{
private:
CPU_rank rank;
int frequency;
float voltage;
public:
CPU (CPU_rank r,int f,float v)
{
rank=r;
frequency=f;
voltage=v;
cout<<"构造了一个cpu!"<<endl;
}
~CPU()
{ cout<<"其构了一个cpu!"<<endl;}
CPU_rank getrank() const
{ return rank;}
void setrank(CPU_rank r)
{ rank=r;}
void run()
{cout<<"cpu开始运行!"<<endl;
}
void stop()
{cout<<"cpu停止运行!"<<endl;}
};
class RAM
{
private:
int ram;
public:
RAM(int r)
{ cout<<"构造了一个ram!"<<endl;}
RAM(){ ram=0;cout<<"构造了一个ram!"<<endl;}
~RAM(){cout<<"析构了一个ram!"<<endl;}
void run()
{cout<<"ram开始运行!"<<endl;}
void stop()
{cout<<"ram停止运行!"<<endl;}
};
class CDRAM
{
private:
int cdram;
public:
CDRAM(int c)
{ cdram=c;
cout<<"构造了一个cdram!"<<endl;}
CDRAM(){ cout<<"构造了一个cdram!"<<endl;}
~CDRAM(){cout<<"析构了一个cdram!"<<endl;}
void run()
{cout<<"cdram开始运行"<<endl;}
void stop()
{cout<<"cdram停止运行"<<endl;}
};
class computer
{private:
CPU cpu;RAM ram;CDRAM cdram;
public:
computer(CPU c,RAM r,CDRAM cd);
computer();
~computer(){cout<<"析构了一个computer"<<endl;}
computer(computer &p);
void run()
{cout<<"computer开始运行"<<endl;}
void stop()
{cout<<"computer停止运行"<<endl;}
};
computer::computer():cpu(p6,100,float(1.3)),ram(20),cdram(30)
{cout<<"构造了一个 cpu00"<<endl;}
computer::computer(CPU c,RAM r,CDRAM cd):cpu(c),ram(r),cdram(cd)
{cout<<"构造了一个computer"<<endl;}
computer::computer(computer &p): cpu(p.cpu),ram(p.ram),cdram(p.cdram)
{ cout<<"调用了复制构造函数"<<endl;}
void main()
{
CPU m(p6,300,float(2.8));
RAM n(1);
CDRAM q(2);
computer com1;
computer com2(m,n,q);
}
// class.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
enum CPU_Rank{p1=1,p2,p3,p4,p5,p6,p7};
class CPU
{
private:
CPU_Rank rank;
int frequency;
float voltage;
public:
CPU(CPU_Rank r, int f, float v)
{
rank = r;
frequency = f;
voltage = v;
cout << "构造了一个CPU!" << endl;
}
~CPU(){ cout << "析构了一个CPU!" << endl; }
CPU_Rank GetRank() const { return rank; }// 外部接口函数
int GetFrequency() const{ return frequency; }
float GetVoltage() const{ return voltage; }
void SetRank(CPU_Rank r){ rank = r; }//
void SetFrequency(int f){ frequency = f; }
void SetVoltage(float v){ voltage = v; }
void Run(){ cout << "CPU开始运行!" << endl; }
void Stop(){ cout << "CPU停止运行!" << endl; }
};
enum RAM_Type{DDR2=2, DDR3, DDR4 };
class RAM
{
private:
int capacity;
RAM_Type type;
float Mfrequency;
public:
RAM(int c, RAM_Type t, float mf)
{
capacity = c;
type = t;
Mfrequency = mf;
cout << "构造了一个RAM!" << endl;
}
~RAM(){ cout << "析构了一个RAM!" << endl; }
RAM_Type GetType() const { return type; }
int GetCapacity() const{ return capacity; }
float GetMfrequency() const { return Mfrequency; }
void SetType(RAM_Type t) { type = t; }
void SetCapacity(int c){ capacity = c; }
void SetMfrequency(float mf){ Mfrequency = mf; }
void Run(){ cout << "RAM开始运行!" << endl; }
void Stop(){ cout << "RAM停止运行!" << endl; }
};
enum CDRAM_Interface{SATA,USB};
enum CDRAM_Install_type{external,built_in};
class CDRAM
{
private:
CDRAM_Interface interface;
int cache_size;
CDRAM_Install_type install_type;
public:
CDRAM(CDRAM_Interface i,int cs,CDRAM_Install_type ty)
{
interface = i;
cache_size = cs;
install_type = ty;
cout << "构造了一个CDRAM!" << endl;
}
~CDRAM(){ cout << "析构了CDRAM!" << endl; }
CDRAM_Interface GetInterface() const { return interface; }
int GetCache_size() const{ return cache_size; }
CDRAM_Install_type GetInstall_type() const { return install_type; }
void SetInterface(CDRAM_Interface i){ interface = i; }
void SetCache_size(int cs){ cache_size = cs; }
void SetInstall_type(CDRAM_Install_type ty){ install_type = ty; }
void Run(){ cout << "CDROM开始运行!" << endl; }
void Stop(){ cout << "CDROM停止运行!" << endl; }
};
class COMPUTER
{
private:
CPU my_cpu;
RAM my_ram;
CDRAM my_cdram;
unsigned int storage_size;
unsigned int bandwidth;
public:
COMPUTER::COMPUTER(CPU c, RAM r, CDRAM cdr, int sto, int ban) :my_cpu(c), my_ram(r), my_cdram(cdr)
{
storage_size = sto;
bandwidth = ban;
cout << "构造了一个COMPUTER!" << endl;
}
~COMPUTER(){ cout << "析构了COMPUTER!" << endl; }
void Run()
{
my_cpu.Run();
my_ram.Run();
my_cdram.Run();
cout << "COMPUTER开始运行!" << endl;
}
void Stop()
{
my_cpu.Stop();
my_ram.Stop();
my_cdram.Stop();
cout << "COMPUTER停止运行!" << endl;
}
};
/*COMPUTER::COMPUTER(CPU c, RAM r, CDRAM cdr, int sto, int ban) :my_cpu(c), my_ram(r), my_cdram(cdr)
{
//my_cpu = c;
//my_ram = r;
//my_cdram = cdr;
storage_size = sto;
bandwidth = ban;
cout << "构造了一个COMPUTER!" << endl;
}*/
int _tmain(int argc, _TCHAR* argv[])
{
CPU a(p6, 300, 220);//声明一个CPU对象,下面都是对这个对象的操作
a.Run();
a.Stop();
cout << "**************************" << endl;
RAM b(1600, DDR3, 220.34);
b.Run();
b.Stop();
cout << "**************************" << endl;
CDRAM c(USB, 400, external);
c.Run();
c.Stop();
cout << "**************************" << endl;
COMPUTER mycomputer(a, b, c, 300, 200);
mycomputer.Run();
mycomputer.Stop();
cout << "**************************" << endl;
return 0;
}
#include <iostream>
using namespace std;
enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
private:
CPU_Rank rank;
int frequency;
float voltage;
public:
CPU (CPU_Rank r, int f, float v)
{
rank = r;
frequency = f;
voltage = v;
cout << "构造了一个CPU!" << endl;
}
CPU()
{
cout << "构造了一个CPU!" << endl;
};
~CPU ()
{
cout << "析构了一个CPU!" << endl;
}
CPU_Rank GetRank() const { return rank; }
int GetFrequency() const { return frequency; }
float GetVoltage() const { return voltage; }
void SetRank(CPU_Rank r) { rank = r; }
void SetFrequency(int f) { frequency = f; }
void SetVoltage(float v) { voltage = v; }
void Run() {cout << "CPU开始运行!" << endl; }
void Stop() {cout << "CPU停止运行!" << endl; }
};
class RAM
{
public:
RAM ()
{
cout << "构造了一个RAM!" << endl;
}
~RAM ()
{
cout << "析构了一个RAM!" << endl;
}
void Run() {cout << "RAM开始运行!" << endl; }
void Stop() {cout << "RAM停止运行!" << endl; }
};
class CDROM
{
public:
CDROM (){ cout << "构造了一个CDROM!" << endl; }
~CDROM ()
{
cout << "析构了一个CDROM!" << endl;
}
void Run() {cout << "CDROM开始运行!" << endl; }
void Stop() {cout << "CDROM停止运行!" << endl; }
};
class COMPUTER
{
private:
CPU cpu;
RAM ram;
CDROM cdrom;
public:
COMPUTER()
{
cout << "构造了一个COMPUTER!" << endl;
}
~COMPUTER ()
{
cout << "析构了一个COMPUTER!" << endl;
}
void Run()
{
cout << "COMPUTER开始运行!" << endl;
cpu.Run();
ram.Run();
}
void Stop()
{
ram.Stop();
cpu.Stop();
cout << "COMPUTER停止运行!" << endl;
}
};
int main()
{
COMPUTER a;
a.Run();
a.Stop();
}
只想说一句,这么多年过去了,我也在写这个代码
class Computer{
private:
Cpu cpu;
Ram ram;
Cdrom cdrom;
public:
void run()
{}
void stop()
{}
}
c语言编写一个函数,求两数中的最小数。在主函数中输入两个数,调用函...
include <stdio.h>#include <math.h>\/\/定义一个函数,求两数中的最小数int min(int x, int y){ if (x < y) \/\/如果x小于y return x; \/\/返回x else \/\/否则 return y; \/\/返回y}int main(){ int a, b; \/\/定义两个整型变量a和b printf("请输入两个整数:\\n"); \/\/提...
c语言定义一个用于输入整数数组元素的函数?
要定义一个用于输入整数数组元素的函数,您可以使用以下C语言代码:在上述代码中,我们定义了一个名为input_array的函数,该函数接受两个参数:一个整数数组arr和一个整数n,表示数组的长度。函数使用for循环迭代数组中的每个元素,并使用scanf函数从用户输入中读取一个整数,并将其存储到当前元素中。在mai...
c预言+请你编写一个函数,输入是一小段的英文,输出是它的英文数量?
最后,函数返回 count 变量,表示输入字符串中的单词数量。在主函数中,我们定义了一个包含输入字符串的字符数组 s,然后将其传递给 count_words() 函数来计算单词数。最后,使用 printf() 函数输出单词数量。
#c语言编写一个函数同时求圆的周长和面积,用指针怎么写?
include <stdio.h> include <math.h> \/\/ 函数声明 void circle_properties(double r, double *p, double *a);int main(void){ double radius, perimeter, area;\/\/ 输入圆的半径 printf("输入圆的半径: ");scanf("%lf", &radius);\/\/ 计算圆的周长和面积 circle_properties(radius, &perimet...
C语言如何声明一个返回函数指针的函数?
C语言指向函数的指针承载的信息比较复杂,组织起来要素要写全。根据指向函数的指针的书写语法,下面的代码就是一个返回函数指针的函数:int (*f(void))(int){\/\/f是函数,没有参数,返回指向一个函数的指针;这个被指向的函数有一个int型形参,且返回一个int值 return fun;\/\/fun是个函数名,必须...
c语言编写一个函数,找出一维数组中的最大值和最小值,并计算出数组元素的...
include "stdio.h"double fun(double a[],int n,int *max,int *min);\/*函数原型声明:其中a为数组,n为数组元素个数,max指向最大数,min指向最小数,函数返回值为平均值*\/ int main(){ double b[10],aver;int x,y=10,max=0,*ma=&max,min=0,*mi=&min;for(x=0;x<y;x++){b...
用C语言编写程序,调用函数求一个圆柱体的表面积和体积
在C语言中,你可以编写一个函数来计算圆柱体的表面积和体积。以下是一个简单的程序示例:首先,我们从定义必要的变量开始,包括圆柱体的半径(r)和高(h),以及表面积(S)和体积(V):include include float calculate_cylinder(float r, float h) { float S = 12.5663704 * r * h; \/\/ ...
用C编写一个简单的窗口函数代码?各位大侠提供一下
这个函数需要调用WindowsAPI。。。有英文注释的。能看懂就看一下吧。要是想写Windows窗口程序建议买本《Windows程序设计》..include <windows.h> \/* Declare Windows procedure *\/ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);\/* Make the class name into a global variable ...
C语言 编写一个函数,由实参传来一个字符串,统计字符串中字母,数字,空 ...
include <stdio.h> include <string.h> int letter,number,blank,other;void count(char str[]){ int i;for(i=0;str[i]!='\\0';i++){ if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))letter++;else if(str[i]>='0'&&str[i]<='9')number++;else...
求C语言所有的简单函数!
读者在编制c程序时可能要用到更多的函数,请查阅有关的Turbo C库函数手册。(1)数学函数使用数学函数时,应该在源文件中使用命令:#include″math.h″ 函数名 函数与形参类型 功能 返回值acos double acos(x)double x 计算cos-1(x)的值-1<=x<=1 计算结果asin double asin(x)double x 计算sin-1(x)的值-...
睢彪休斯: C/C++ code #include "iostream" #include "string" using namespace std; class Client { public : static void ChangeServerName(string strName) { ServerName = strName; } Client() { ++ClientNum; } ~Client() { --ClientNum; } static string ...
临沂市17875459033: 声明一个简单的Computer类,有数据成员cpu、ram和cdrom等,有两个公有成员run()和stop(),其中cpu、ram和cdrom分别为CPU、RAM和CDROM类的一个对象.声明并实现 - ?
睢彪休斯: #include using namespace std; class CPU { public: CPU(){cout
临沂市17875459033: 声明一个简单的Computer类,数据成员包括微处理器,内存,光驱等,有两个共有成员函数run和stop的C++程序设计 - ?
睢彪休斯: public class computer() {private: int cpu,memory,CDROM; public: void run(){cpu=1; memory=1; CDROM=1;} void stop(){ cpu=0; memory=0; CDROM=0;} }
临沂市17875459033: 声明语句chars[20]=computer,能用下面语句替代吗?为什么?chars[20],s=computer; - ?
睢彪休斯: 你的写法有误,应该是 char s[20]="computer"; char s[20],s="computer"; 这样是等价的
临沂市17875459033: 求解一个c++面向对象的简单程序 - ?
睢彪休斯: #include enum CPU_rank{p1=1,p2,p3,p4,p5,p6,p7}; class CPU { private: CPU_rank rank; int frequency; float voltage; public: CPU (CPU_rank r,int f,float v) { rank=r; frequency=f; voltage=v; cout< } ~CPU() { cout< CPU_rank getrank() const { return ...
临沂市17875459033: C++编程小题4?
睢彪休斯: 我晕,都是你的...我都接了.加我Q:646203846,我给你慢慢说.. 这样,满意不? #include<iostream> using namespace std; class RAM { private: int ram; }; class CDRAM { private: int cdrom; }; class CPU { private: int cpu; }; class ...
临沂市17875459033: c++面向对象与程序设计 - ?
睢彪休斯: //第一问: #include <iostream>#include <string> using namespace std; //调用顺序已按数字标出 class CPU { public: CPU(){}//2 ~CPU(){}//8 }; class RAM { public: RAM(){}//3 ~RAM(){}//7 }; class CDROM { public: CDROM(){}//4 ~CDROM(){}//6 }; ...
临沂市17875459033: 设有一个字符串“this is a computer”,用函数编程求出字符i在字符串首次出现的位置?
睢彪休斯: #include<stdio.h> #include<string.h> int main() { char str[256]; int i,count=0; gets(str); for(i=0;i<strlen(str);i++) { if(str[i]=='i') { count++; break; } else count++; } printf("字符i在字符串中首次出现在%d位置!\n",count); return 0; }
临沂市17875459033: 谁知道如何将一个类的数据成员定义为另一类的对象? - ?
睢彪休斯: 就是 RAM ram; CPU cpu;这样就是给他实例化了 构造函数带4个参数,分别给CPU类、RAM类的构造函数传参数 应该是这样的 Computer { cpu=new CPU(str1,str2,str3); ram=new RAM(str1,str2,str3) } 当然..这只是我的一个思路..你看对你帮助不.
临沂市17875459033: C++关于友元函数的一问一答,求解释. - ?
睢彪休斯: #include <iostream> using namespace std; class aclass { int id; public: friend void afriendfun(aclass thecls); friend void afriendfun(aclass thecls, int aid); void setid( int aid ); int getid() {return id;} }; void aclass::setid(int aid) { id = aid; } void afriendfun(...