Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C++
#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;
}

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 :-(
Posted
Comments
PIEBALDconsult 28-Aug-15 16:18pm    
Please read up on the fundamentals of OOP -- encapsulation, inheritance, and polymorphism.

 
Share this answer
 
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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900