如何从类中调用不同的参数以实现不同的功能 [英] How to call different parameters from a class for different function

查看:68
本文介绍了如何从类中调用不同的参数以实现不同的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很少关于类的教程,但是所有这些教程都显示了使用其所有参数的函数(比如这个)。

这是我从cplusplus.com中获取的一个例子

I have watch few tutorials about classes but all of them show the functions that uses all of its parameters (such as this).
This is an example I took from cplusplus.com

#include "stdafx.h"
#include <iostream>
using namespace std;

class Rectangle
	{
		double width, height;
	public:
		void setValues ( double, double );

		double area ()
		{
			return width * height;
		}
	};

void Rectangle::setValues ( double x, double y )
{
	width = x;
	height = y;
}

int main ()
{
	Rectangle rect;
	double x;
	double y;
	cout << "Enter the height: ";
	cin >> x;
	cout << "Enter the width: ";
	cin >> y;
	rect.setValues(x, y);
	cout << "The area of your rectangle is " << rect.area() << "." << endl;
	return 0;
}



如何创建一个具有多个参数和多个类型(int,float)的类,以后可以通过不同的函数进行部分调用?



某些形状需要不同的参数,如宽度,高度,半径和弧度。但是没有一个同时需要所有人。例如,矩形函数将使用宽度和高度。但是要计算一个圆,它只需要半径参数。



这只是一个例子,因为它只能在每个函数中省去一些重复的行。但我计划在更复杂的问题上尝试它。



我是编程新手。请原谅我提出这个幼稚的问题: - (


How can I create a class with multiple parameters and multiple type (int, float) that can be partially call later by different function?

Some shape require different parameters such as width, height, radius, and radians. But none requires all at the same time. For instance, a rectangle function will use width and height. But to calculate a circle, it will only require radius parameter.

This is a mere example as it could only save me a few repetitive line in each functions. But I have plans to try it in more complex problems.

I'm new to programming. Forgive me for asking this childish question :-(

推荐答案

https://www.google.com/search?#q=shape+class+c%2B%2B [ ^ ]。


使用函数重载的概念...使用FO我们可以创建具有不同参数和不同类型的函数,稍后可以根据参数类型和所需任务调用。



下面的示例显示了根据参数和类型的数量为每个形状使用函数重载概念的程序



#includeiostream

#includeconio.h

使用命名空间std;

class measure

{

public:

void shape(int r);

void shape(int l,int b);

void shape(float t,在td,int e);

void shape(长a);

void shape(float c,long int g);

void shape (双j);

void shape(float h,double f);

};

void measure :: shape(int r)

{

cout<<圈子的区域是<<<<<<<<<<<<<<<<<<<<<<<<< >
void measure :: shape(int l,int b)

{

cout<<矩形区域为<< l * b;

}

void measure :: shape(float t,int d,int e)

{

cout<<三角形的区域是<< t * d * e;

}

void measure :: shape(long a)

{

cout<<广场的面积是<< a * a;

}

void measure :: shape(float c,long int g)

{

cout<<锥体的体积为<<(1 / 3)* 3.14 * c * c * g;

}

无效测量::形状(双j)

{

cout<<球体的体积是<<(4/3)* 3.14 * j * j * j;

}

void measure :: shape(float h,double f)

{

cout<<\气缸的nVolume是<<<<<<<<<<<<<<<<<<<<<< 3.14 * f * f * h;

}

int main()

/>
int r,d,e,l,b;

浮动t,c,h;

长a;

int ch;

double j,f;

long int g;

measure obj;

cout< <\tCALCULATION of AREA and VOLUME;

cout<<\ n \ nn1。圈子区域;

cout<<\ n2。矩形区域;

cout<<\ n3。三角区;

cout<<\ n4。广场面积;

cout<<\ n5。锥体的体积;

cout<<\ n6。球体的体积;

cout<<\ n7。气瓶的容量;

cout<<\ n\t输入您的选择;

cin>> ch;

开关(ch)

{

案例1:

cout<<输入圆的半径值\ n ;

cin>> r;

obj.shape(r);

休息;

案例2 :

cout<<输入矩形的边\ n;

cin>> l>> b;

obj.shape(l,b);

休息;

案例3:

cout<<进入三角形的两侧\ n;

cin>> d>> e;

obj.shape(0.5,d,e);

break;

案例4:

cout<<进入广场两侧;

cin>> a;

obj.shape(a);

休息;

案例5:

cout<<\ n输入半径锥体;

cin>> c;

cout<<\ n输入th的高度e cone;

cin>> g;

obj.shape(c,g);

break;

案例6:

cout<<\ n输入半径;

cin>> b;

obj。形状(b);

休息;

案例7:

cout<<\ n输入半径;

cin>> f;

cout<<\ n输入高度;

cin>> h;

obj.shape(h,f);

break;

默认值:

cout<<\ n输入的选项是错误的选择;

}

getch();
use the concept of function overloading ... with FO we can create functions with different parameters and different types and can be called later based on the parameter type and the task required.

Example below shows the program which uses the concept of Function Overloading for each shapes based on the number of parameters and types

#include "iostream"
#include "conio.h"
using namespace std;
class measure
{
public:
void shape(int r);
void shape(int l,int b);
void shape(float t,int d,int e);
void shape(long a);
void shape(float c, long int g);
void shape(double j);
void shape(float h, double f);
};
void measure::shape(int r)
{
cout<<"area of the circle is "<<3.14*r*r;
}
void measure::shape(int l,int b)
{
cout<<"area of the rectangle is"<<l*b;
}
void measure::shape(float t,int d,int e)
{
cout<<"area of the triangle is"<<t*d*e;
}
void measure::shape(long a)
{
cout<<"area of the square is"<<a*a;
}
void measure::shape(float c, long int g)
{
cout<<"Volume of the cone is "<<(1/3)*3.14*c*c*g;
}
void measure::shape(double j)
{
cout<<"Volume of the sphere is "<<(4/3)*3.14*j*j*j;
}
void measure::shape(float h, double f)
{
cout<<"\nVolume of the Cylinder is "<<3.14*f*f*h;
}
int main()
{
int r,d,e,l,b;
float t,c,h;
long a;
int ch;
double j,f;
long int g;
measure obj;
cout<<"\tCALCULATION OF AREA AND VOLUME";
cout<<"\n\n1. area of circle";
cout<<"\n2. area of rectangle";
cout<<"\n3. area of triangle";
cout<<"\n4. area of square";
cout<<"\n5. Volume of the cone";
cout<<"\n6. Volume of the sphere";
cout<<"\n7. Volume of the cylinder";
cout<<"\n\tEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the value of radius of the circle \n";
cin>>r;
obj.shape(r);
break;
case 2:
cout<<"enter the sides of rectangle \n";
cin>>l>>b;
obj.shape(l,b);
break;
case 3:
cout<<"enter the sides of triangle \n";
cin>>d>>e;
obj.shape(0.5,d,e);
break;
case 4:
cout<<"enter the sides of square";
cin>>a;
obj.shape(a);
break;
case 5:
cout<<"\nEnter the radius of the cone";
cin>>c;
cout<<"\nEnter the height of the cone";
cin>>g;
obj.shape(c,g);
break;
case 6:
cout<<"\nEnter the radius";
cin>>b;
obj.shape(b);
break;
case 7:
cout<<"\nEnter the radius";
cin>>f;
cout<<"\nEnter the height";
cin>>h;
obj.shape(h,f);
break;
default:
cout<<"\nThe choice entered is a wrong choice";
}
getch();


这篇关于如何从类中调用不同的参数以实现不同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆