Hi the code below is a simple class example in C
#include <iostream>
using namespace std;
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
// W3Schools
As I see it, the way you can define an object is to write it in code like : <ClassName> <ObjectName>
Now my question is : Is there a way to input an object? like cin >> ObjectName;
And after we entered the name of object, we enter the parameters.
Is it possible at all?
CodePudding user response:
You can't enter an object name from the command line, however, you can create a default constructed object and then fill its' properties from the input by overloading operator>>
.
class Car {
public:
Car() = default;
// ...
};
std::istream& operator>>(std::istream& in, Car& car) {
in >> car.brand >> car.model >> car.year;
return in;
}
int main() {
Car carObj1;
cout << "Enter a car brand, model, and year: ";
cin >> carObj1;
// ...
}
CodePudding user response:
Yes, There is operator overloading in C and you can overload both << and >> operators. Here is an example code:
#include <iostream>
using namespace std;
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
Car() = default;
void Carinput(std::istream& is)
{
is >> brand >> model >> year;
}
void carOutput(std::ostream& os) const
{
os << brand << " " << model << " " << year << std::endl;
}
};
std::istream &operator>>(std::istream &is,Car &car)
{
car.Carinput(is);
return is;
}
std::ostream &operator<<(std::ostream &os,const Car &car)
{
car.carOutput(os);
return os;
}
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
std::cout << "\n*============================================*\n"
<< std::endl;
std::cout << "Your input:" << std::endl;
Car carObj3;
std::cin >> carObj3;
std::cout << carObj3;
return 0;
}
CodePudding user response:
This is one of the ways of doing it:
#include<iostream>
#include<vector>
class Car
{
public:
Car( const std::string& brand, const std::string& model, int year )
: m_brand( brand ), m_model( model ), m_year( year ) // initialize members like this
{
}
std::string m_brand;
std::string m_model;
int m_year;
};
int main ()
{
std::vector< Car > cars; // use an std::vector to store the objects
std::string brand( "" );
std::string model( "" );
std::string year( "" );
std::cout << "How many cars do you want to add? ";
std::string count( "" );
std::getline( std::cin, count ); // get input as string using std::getline()
int carCount { std::stoi( count ) }; // converts it to type int before
// assigning it to carCount
cars.reserve( carCount ); // reserve some space in vector to avoid
// unnecessary allocations and slowdowns
for ( int idx = 0; idx < carCount; idx )
{
std::cout << "Enter the brand name of car number " << idx 1 << ": ";
std::getline( std::cin, brand );
std::cout << "Enter the model name of car number " << idx 1 << ": ";
std::getline( std::cin, model );
std::cout << "Enter the production year of car number " << idx 1 << ": ";
std::getline( std::cin, year );
cars.emplace_back( Car( brand, model, std::stoi( year ) ) ); // push the new Car
} // instance to the
// vector called cars
std::cout << '\n';
for ( int idx = 0; idx < carCount; idx )
{
std::cout << "Info for car number " << idx 1 << ": "
<< cars[ idx ].m_brand << " " << cars[ idx ].m_model << " " << cars[ idx ].m_year << "\n";
}
return 0;
}
Here is the output:
How many cars do you want to add? 2
Enter the brand name of car number 1: Porsche
Enter the model name of car number 1: GT3 RS
Enter the production year of car number 1: 2019
Enter the brand name of car number 2: BMW
Enter the model name of car number 2: M6
Enter the production year of car number 2: 2016
Info for car number 1: Porsche GT3 RS 2019
Info for car number 2: BMW M6 2016
But keep in mind that this class still needs a lot of work like adding member functions (like getters and setters) etc. So this is not how I write classes. I just wanted to keep it simple so you can easily understand the concepts.