Simple Question about C++ classes

  • C/C++
  • Thread starter spaghetti3451
  • Start date
  • Tags
    C++ Classes
In summary: This allows for more flexibility and versatility in the use of methods within the program. In summary, the argument(s) of a method can be any type of object, not just the predefined types, and this includes objects from other classes that can be accessed by the class in which the method is defined.
  • #1
spaghetti3451
1,344
33
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
  • #2
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.
 

1. What is a class in C++?

A class in C++ is a user-defined data type that contains data members (variables) and member functions (methods). It serves as a blueprint for creating objects, which are instances of the class.

2. How do I declare a class in C++?

To declare a class in C++, you use the class keyword followed by the name of the class, and then enclose the data members and member functions within curly braces. For example: class Person { // data members and member functions go here };

3. What is the difference between a class and an object?

A class is a template or blueprint for creating objects, while an object is an instance of a class. In other words, a class defines the structure and behavior of objects, and objects are the actual entities that hold data and perform operations.

4. How do I access the member variables of a class?

You can access the member variables of a class by using the dot operator (.) after an object of the class. For example, if you have a class called Car with a member variable called color, you can access it like this: Car myCar; myCar.color = "red";

5. Can a class have more than one constructor?

Yes, a class can have multiple constructors in C++. This is known as constructor overloading. Each constructor can have a different set of parameters, allowing you to initialize objects in different ways.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
2
Replies
41
Views
3K
  • Programming and Computer Science
Replies
19
Views
969
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
Back
Top