What Types Can Be Arguments in C++ Class Methods?

  • Context: C/C++ 
  • Thread starter Thread starter spaghetti3451
  • Start date Start date
  • Tags Tags
    C++ Classes
Click For Summary
SUMMARY

In C++, class methods can accept arguments of any type, provided the class of the object is accessible within the program. This includes not only built-in types such as int, char, long, float, and double, but also user-defined types, as long as they are made available to the method. The discussion clarifies that the flexibility of argument types in methods extends beyond just the class itself and standard types, allowing for a broader range of objects to be utilized as parameters.

PREREQUISITES
  • Understanding of C++ class structures
  • Familiarity with object-oriented programming concepts
  • Knowledge of built-in data types in C++
  • Ability to access and include classes in C++
NEXT STEPS
  • Explore C++ class member functions and their parameters
  • Learn about C++ templates for generic programming
  • Investigate the use of polymorphism in C++
  • Study the implications of passing objects by reference vs. by value in C++
USEFUL FOR

C++ developers, software engineers, and students learning object-oriented programming who want to deepen their understanding of method arguments and type flexibility in class design.

spaghetti3451
Messages
1,311
Reaction score
31
I have seen the following in a abook:

"The argument(s) of a method can be any type of object (as long as its class has been made available to the program), as well as the predefined default types of variable. For example, methods in our three-vector class are not limited to just accepting arguments of type threevector, int, double, etc."

I understand that the first statement is telling us that objects of all classes made available to the program can be used as arguments of a method.

What I do not understand, however, is how the second statement follows logically from the first. "Methods in our three-vector class are not limited to just accepting arguments of type threevector, int, double, etc." Sure! No problem! But what other types of objects are you thinking of?
 
Technology news on Phys.org
failexam said:
I have seen the following in a abook:

"The argument(s) of a method can be any type of object (as long as its class has been made available to the program), as well as the predefined default types of variable. For example, methods in our three-vector class are not limited to just accepting arguments of type threevector, int, double, etc."

I understand that the first statement is telling us that objects of all classes made available to the program can be used as arguments of a method.

What I do not understand, however, is how the second statement follows logically from the first. "Methods in our three-vector class are not limited to just accepting arguments of type threevector, int, double, etc." Sure! No problem! But what other types of objects are you thinking of?
The second sentence is saying that in addition to the built-in types (int, char, long, float, double, etc.) and types declared in the same class, objects of other classes can be used as parameters, as long as these other classes can be accessed by the class in which the method is defined.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K