Whats wrong with my class [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I don't know why but when i create an object with my class and use the default constructor, when i try to pass the variable named cash to the accessor user2.setCash(cash) which purpose is to primary set cash equal to new_cash, it gives a large value like 1.222256e+461 or something like that. Why does that happen? If i use my overload constructor it works fine.
main.cpp
#include <iostream>
#include <string>
#include "Bank.h"
using namespace std;
int main()
{
string name;
int id;
double cash;
bank user2;
cout << "\n\nPlease type your name: ";
getline(cin >> ws, name);
user2.setName(name);
cout << "Enter an id number: ";
cin >> id;
user2.setID(id);
cout << "Enter your cash: ";
cin >> cash;
cout << cash << endl;
user2.setCash(cash);
cout << "\nAlright " << user2.getName() << ", current cash: " << user2.getCash();
cout << "\nChoose how much would you like to Deposit: ";
cin >> cash;
user2.deposit(cash);
cout << "New amount is: " << user2.getCash() << " For user ID: " << user2.getID() << "\n\n";
bank::printStatic();
return 0;
}
Bank.h
#ifndef BANK_H
#define BANK_H
#include <iostream>
#include <string>
using namespace std;
class bank
{
public:
// Default Constructor
bank();
// Overload Constructor
bank(string, int, double);
// Destructor
~bank();
// Accessor Functions
string getName() const;
int getID() const;
double getCash() const;
// Mutator Functions
void setName(string);
void setID(int);
void setCash(double);
// Functions
void withdraw(double);
void deposit(double);
static void printStatic();
private:
// Member Variables
string new_name;
int new_id;
double new_cash;
// Static Member Variables
static int num_of_accounts;
static double total_cash;
};
#endif
Bank.cpp
#include "Bank.h"
// Static Variables
int bank::num_of_accounts = 0;
double bank::total_cash = 0.0;
// Default Constructor
bank::bank()
{
int new_id = 0;
double new_cash = 0.0;
++num_of_accounts; // New object is created e.g. a person so total accounts must be increased.
}
// Overload Constructor
bank::bank(string name, int id, double cash)
{
new_name = name;
new_id = id;
new_cash = cash;
++num_of_accounts; // New object is created e.g. a person so total accounts must be increased.
total_cash += new_cash; // New object is created e.g. a person so his/hers cash must be added to the total cash of the bank.
}
// Destructor
bank::~bank()
{
--num_of_accounts; // When Destructor is called to destroy an object (e.g. a person) then the id must be dropped by 1 cause the person e.g. (left).
total_cash -= new_cash; // And the balance he had to be removed so it is not counted in total cash avaliable in the bank cause he e.g. (left).
}
// Accessor Functions
string bank::getName() const
{
return new_name;
}
int bank::getID() const
{
return new_id;
}
double bank::getCash() const
{
return new_cash;
}
// Mutator Functions
void bank::setName(string name)
{
new_name = name;
}
void bank::setID(int id)
{
new_id = id;
}
void bank::setCash(double cash)
{
cout << new_cash << endl;
total_cash -= new_cash; // We must remove his prior cash which we holded in the total so we can then use the new value suplied.
new_cash = cash;
total_cash += new_cash; // Here we add the new cash (balance) he/she has.
}
void bank::withdraw(double cash)
{
new_cash -= cash;
total_cash -= cash;
}
void bank::deposit(double cash)
{
new_cash += cash;
total_cash += cash;
}
void bank::printStatic()
{
cout << "Total users are: " << num_of_accounts << endl;
cout << "Total cash in bank is: " << total_cash << endl;
}

You need to initialize all primitive type members in the constructor.
Otherwise you get indeterminate values
Also, the non-default constructor is buggy:
// Default Constructor
bank::bank()
{
int new_id = 0;
double new_cash = 0.0;
....
^ sets values of local variables, not the member variables
I'd suggest to use the initilization lists:
// Default Constructor
bank::bank() : new_name(), new_id(0), new_cash(0.0)
{
++num_of_accounts;
}
// Overload Constructor
bank::bank(string name, int id, double cash)
: new_name(name), new_id(id), new_cash(cash)
{
++num_of_accounts;
total_cash += new_cash;
}
You could also combine the two:
bank::bank(string name = "", int id = 0, double cash = 0.0)
: new_name(name), new_id(id), new_cash(cash)
{
++num_of_accounts;
total_cash += new_cash;
}

In your default constructor, you're declaring local variables with the same names as the member variables. Then you're setting these local variables instead of assigning the members. Get rid of the type declarations so they're normal assignments.
bank::bank()
{
new_id = 0;
new_cash = 0.0;
++num_of_accounts; // New object is created e.g. a person so total accounts must be increased.
}

Related

Artwork label (Artist.cpp Isn't printing)

main.cpp
#include "Artist.h"
#include "Artwork.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string userTitle, userArtistName;
int yearCreated, userBirthYear, userDeathYear;
getline(cin, userArtistName);
getline(cin, userTitle);
cin >> userBirthYear;
cin >> userDeathYear;
cin >> yearCreated;
Artist userArtist = Artist(userArtistName, userBirthYear, userDeathYear);
Artwork newArtwork = Artwork(userTitle, yearCreated, userArtist);
newArtwork.PrintInfo();
}
Artwork.cpp
#include "Artwork.h"
#include <iostream>
// TODO: Define default constructor
Artwork::Artwork(){
title = "Unknown";
yearCreated = -1;
}
// TODO: Define second constructor to initialize
// private fields (title, yearCreated, artist)
Artwork::Artwork(string title, int yearCreated, Artist artist){
this->title = title;
this->yearCreated = yearCreated;
this->artist = artist;
}
// TODO: Define get functions: GetTitle(), GetYearCreated()
string Artwork::GetTitle(){
return title;
}
int Artwork::GetYearCreated(){
return yearCreated;
}
// TODO: Define PrintInfo() function
// Call the PrintInfo() function in the Artist class to print an artist's information
void Artwork::PrintInfo(){
cout << "Title: " << title << ", " << yearCreated<< endl;
}
Artwork.h
#ifndef ARTWORKH
#define ARTWORKH
#include "Artist.h"
class Artwork{
public:
Artwork();
Artwork(string title, int yearCreated, Artist artist);
string GetTitle();
int GetYearCreated();
void PrintInfo();
private:
// TODO: Declare private data members - title, yearCreated
string title;
int yearCreated;
// TODO: Declare private data member artist of type Artist
Artist artist;
};
#endif
Artist.h
#ifndef ARTISTH
#define ARTISTH
#include <string>
using namespace std;
class Artist{
public:
Artist();
Artist(string artistName, int birthYear, int deathYear);
string GetName() const;
int GetBirthYear() const;
int GetDeathYear() const;
void PrintInfo() const;
private:
// TODO: Declare private data members - artistName, birthYear, deathYear
string artistName;
int birthYear;
int deathYear;
};
#endif
Artist.cpp
#include "Artist.h"
#include <iostream>
#include <string>
using namespace std;
// TODO: Define default constructor
Artist::Artist(){
artistName = "Unknown";
birthYear = -1;
deathYear = -1;
}
// TODO: Define second constructor to initialize
//private fields (artistName, birthYear, deathYear)
Artist::Artist(string artistName, int birthYear, int deathYear){
this->artistName = artistName;
this->birthYear = birthYear;
this->deathYear = deathYear;
}
// TODO: Define get functions: GetName(), GetBirthYear(), GetDeathYear()
string Artist::GetName() const{
return artistName;
}
int Artist::GetBirthYear()const {
return birthYear;
}
int Artist::GetDeathYear() const{
return deathYear;
}
// TODO: Define PrintInfo() function
void Artist::PrintInfo() const{
if( birthYear >=0 && deathYear == -1){
cout << "Artist: " << artistName << "(" << birthYear << "to present"<< ")"<< endl;
}
else if(birthYear >=0 && deathYear >=0){
cout << "Artist: " << artistName << "(" << birthYear << "to " << deathYear << ")"<< endl;
}
else{
cout << "Artist: " << artistName << " (" << "unknown" << ")"<< endl;
}
}
Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "unknown" and the years of birth and death to -1. PrintInfo() displays "Artist:", then a space, then the artist's name, then another space, then the birth and death dates in one of three formats:
(XXXX to YYYY) if both the birth and death years are nonnegative
(XXXX to present) if the birth year is nonnegative and the death year is negative
(unknown) otherwise
Complete the Artwork class (in files Artwork.h and Artwork.cpp) with constructors to initialize an artwork's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the title to "unknown", the year created to -1. PrintInfo() displays an artist's information by calling the PrintInfo() function in the Artist class, followed by the artwork's title and the year created. Declare a private field of type Artist in the Artwork class.
Ex: If the input is:
Pablo Picasso
Three Musicians
1881
1973
1921
1881 and 1973 being the birth and death years respectively, with 1921 being the year the work was created, the output is:
Artist: Pablo Picasso (1881 to 1973)
Title: Three Musicians, 1921
My issue Is when I test this input I only get... (in a different .h file)
Title: Distant Muses, 2000
I was wondering why void Artist::PrintInfo() wont output anything and would be great if anyone could guide me to the right direction!
Thank You in advance!
Reading through description, your Artwork class should contain PrintInfo() method, which on it's own should call Artist::PrintInfo() once invoked. Add printing the artist info before printing it's own info in the Artwork::PrintInfo() method and you should be good to go. It should look like this:
void Artwork::PrintInfo(){
artist.Printinfo(); // Not 100% sure this is correct syntax, check carefully.
cout << "Title: " << title << ", " << yearCreated<< endl;
}
Also, worth noting that you can 'merge' constructors. Instead of this(Yes I'm aware it's 'required', but you can take a step further):
Artist::Artist(){
artistName = "Unknown";
birthYear = -1;
deathYear = -1;
}
// TODO: Define second constructor to initialize
//private fields (artistName, birthYear, deathYear)
Artist::Artist(string artistName, int birthYear, int deathYear){
this->artistName = artistName;
this->birthYear = birthYear;
this->deathYear = deathYear;
}
You can have this:
Artist::Artist(string artistName="Unknown", int birthYear=-1, int deathYear=-1){
this->artistName = artistName;
this->birthYear = birthYear;
this->deathYear = deathYear;
}
By setting default values, if constructor isn't provided any value, aka default constructor is used, everything will be set to default value, in this case "Unknown" and -1's. If someone provides name, you will have name and -1's and so on.

Why this gross salary function returning 0? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Write a program to
-> create a class “employee” with data member as name, designation, and basic salary and gross salary.
-> Create member functions as function getdata to read and function showdata to display details.
-> Create sum() as friend function to calculate gross salary.
BS=basic_salary
gs = basic_salary + 0.5 * basic_salary + 0.9 * basic_salary;
There's something wrong in my code. The get_gs() function is always returning 0. What is my error?
I have made it run again after deleting the previous .exe too, asumming compiler error. But it remains the same.
#include <iostream>
using namespace std;
class employee
{
string name;
string designation;
double bs;
double gs;
public:
employee()
{
}
employee(string _name, string des, double _bs)
{
name = _name;
designation = des;
bs = _bs;
}
void set(string _name, string des, double _bs)
{
employee(_name, des, _bs);
}
double get_gs()
{
double gs;
gs=bs + (0.5 * bs) + (0.9 * bs);
return gs;
}
};
int main()
{
employee *e = new employee;
string name, desti;
double bs, gs;
cout << "Enter name destiny basic_salary " << endl;
cin >> name >> desti >> bs;
e->set(name, desti, bs);
gs=e->get_gs();
cout << "the Gross salary :" << gs << endl;
return 0;
}
Solution with comments in the code:
#include <iostream>
// using namespace std; // not a good practise
class employee {
std::string name;
std::string designation;
double bs;
// double gs; // wasn't used so remove it
public:
// initialize values like this, and pass strings as "const&":
employee(const std::string& _name, const std::string& des, double _bs) :
name(_name), designation(des), bs(_bs)
{}
employee() : employee("", "", 0) {} // delegate to first constructor
void set(const std::string& _name, const std::string des&, double _bs) {
// your old set() created a temporary employee that
// wasn't used for anything and then it was destroyed
name = _name;
designation = des;
bs = _bs;
}
// make member functions that does not change the state of "this" const
double get_gs() const { return bs + (0.5 * bs) + (0.9 * bs); }
};
int main() {
employee e; // no need for new, create it like any other variable
std::string name, desti;
double bs, gs;
std::cout << "Enter name destiny basic_salary\n";
std::cin >> name >> desti >> bs;
e.set(name, desti, bs);
gs = e.get_gs();
std::cout << "the Gross salary : " << gs << "\n";
}
In your set() method, calling employee(_name, des, _bs); creates a new temporary employee object that is then immediately discarded. It does not update the members of the employee object that set() is called on, like you are expecting. As such, the bs member is never being assigned a value.
Try this instead:
#include <iostream>
using namespace std;
class employee
{
string name;
string designation;
double bs;
public:
employee() : bs(0.0) { }
employee(string _name, string des, double _bs)
: name(_name), designation(des), bs(_bs)
{
}
void set(string _name, string des, double _bs)
{
name = _name;
designation = des;
bs = _bs;
}
double get_gs() const
{
return bs + (0.5 * bs) + (0.9 * bs);
}
};
int main()
{
employee e;
string name, desti;
double bs, gs;
cout << "Enter name destiny basic_salary " << endl;
cin >> name >> desti >> bs;
e.set(name, desti, bs);
gs = e.get_gs();
cout << "the Gross salary :" << gs << endl;
return 0;
}
Alternatively:
int main()
{
string name, desti;
double bs, gs;
cout << "Enter name destiny basic_salary " << endl;
cin >> name >> desti >> bs;
employee e(name, desti, bs);
gs = e.get_gs();
cout << "the Gross salary :" << gs << endl;
return 0;
}

Error with calling a method from another method in the same class

I can't seem to figure out what is going on with this GPA calculator program I'm trying to build. The problem is in the printTranscript method of Student class. The method calls getGPA method of the same class, which returns a double, the output is some massive number instead of a standard GPA.
main.cpp:
#include <iostream>
#include "Student.cpp"
using namespace std;
int main(){
Student stud("Lebron", 23232);
stud.addCourse("Passing", 3, 'A');
stud.addCourse("Finals Record", 4, 'D');
stud.printTranscript();
return 0;
}
Student.h:
#include "Course.h"
#include <vector>
class Student{
private:
string name;
int studentID;
vector<Course> courses;
public:
Student(){
name = "No Name";
studentID = 0;
}
Student(string n, int ID){
name = n;
studentID = ID;
}
string getName(){
return name;
}
void setName(string n){
name = n;
}
int getID(){
return studentID;
}
void setID(int ID){
studentID = ID;
}
void addCourse(string, int, char);
void addCourse(Course);
double getGPA();
void printTranscript();
};
Student.cpp:
#include "Student.h"
void Student::addCourse(string name, int credits, char grade){
courses.push_back(Course(name,credits,grade));
}
void Student::addCourse(Course c){
courses.push_back(c);
}
double Student::getGPA(){
double gradePoints, totalCredits;
for(int i = 0; i < courses.size(); i++){
if(courses[i].getGrade() == 'A'){
gradePoints += (4.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
else if(courses[i].getGrade() == 'B'){
gradePoints += (3.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
else if(courses[i].getGrade() == 'C'){
gradePoints += (2.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
else if (courses[i].getGrade() == 'D'){
gradePoints += (1.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
}
return (gradePoints / totalCredits);
}
void Student::printTranscript(){
cout << "Transcript for: " << name << endl;
cout << "============================" << endl;
for(int i = 0; i < courses.size(); i++){
cout << "Course: " << courses[i].getID() << endl;
cout << "Grade: " << courses[i].getGrade() << endl;
cout << endl;
}
cout << "Overall GPA: " << getGPA() << endl;
}
Course.h:
#include <iostream>
#include <string>
using namespace std;
class Course{
private:
string courseID; // Course name
int numCredits; // Instructor
char letterGrade; // Textbook
public:
Course(string ID, int credits, char grade){ // Assign the course name.
courseID = ID;
numCredits = credits;
letterGrade = grade;
}
string getID(){
return courseID;
}
void setID(string ID){
courseID = ID;
}
int getCredits(){
return numCredits;
}
void setCredits(int credits){
numCredits = credits;
}
char getGrade(){
return letterGrade;
}
void setGrade(char grade){
letterGrade = grade;
}
};
Sorry for the long bit of code, but I can't seem to figure out why output in main is giving me:
Transcript for: Lebron James
============================
Course: Passing
Grade: A
Course: Finals Record
Grade: D
Overall GPA: 2.2321e+230
If I cout stud.getGPA in the main class as a separate statement, it works fine. What explains the massively large number being output here when called from another method of the same class, and how is this fixed?
Sorry for the long code, but I didn't want to miss anything, as I am still at the beginning stages of C++.
From dcl.init/7:
To default-initialize an object of type T means:
If T is a (possibly cv-qualified) class type, constructors are considered. The applicable constructors are enumerated
([over.match.ctor]), and the best one for the initializer () is chosen
through overload resolution. The constructor thus selected is called,
with an empty argument list, to initialize the object.
If T is an array type, each element is default-initialized.
Otherwise, no initialization is performed.
You are directly doing some operation +=:
totalCredits += courses[i].getCredits();
but forget to initialize the variable. Don't assume everything will be set to zero initially.
Thus, Initialize totalCredits to 0.0.
Adding -Wuninitialized:
As suggested by #1201ProgramAlarm, use -Wuninitialized to flag uninitialized variables.
warning: 'totalCredits' may be used uninitialized in this function [-Wuninitialized]
If you want to treat warnings as errors, just add the flag -Werror

c++ bank account array, looping through

I think I need to perform a multidimensional array or vector within it will define account and balance. Such as I defined {acctnum=44421, balance=0} by default the balance would be 0, and then I want to define another account {acctnum=55531, balance=""}. With each of these I need to take the first account with deposit and perform a withdrawal 10 then deposit 6. Then go to second deposit 3 and then deposit 5. Then display the intial deposit, then after withdrawal and after second deposit and what the account is after these actions.
header
#ifndef account_h_
#define account_h_
#include <iostream>
#include <string>
#include <account>
using std::account;
class account
{
public:
account();
account(const std::string acctnum);
~account();
//----------------------------------------------------
//account number
const std:string get_acctnum() const{
return m_acctnum;
}
//----------------------------------------------------
//deposit
void deposit(float amt){
m_balance += amt;
}
//----------------------------------------------------
//withdraw
void withdraw(float amt){
m_balance -= amt;
}
//----------------------------------------------------
//current balance
const double get_balance() const{
return m_balance;
})
//----------------------------------------------------
//display string
const std::string asString() const;
private:
//----------------------------------------------------
//account number
int m_acctnum;
//----------------------------------------------------
//balance
double m_balance;
//----------------------------------------------------
//after deposit or withdrawal
void inc(const double d) {
m_reading += d;
}
};
#endif
program
#include <iostream>
#include <string>
#include <stdlib.h>
#include "account.h"
using namespace std;
//----------------------------------------------------
//bank account default balance
account::account(){
m_balance = 0;
m_acctnum = "???";
}
account::account(const std::string acctnum){
m_balance = 0;
m_acctnum = acctnum;
}
account::~account(){
}
//----------------------------------------------------
//deposit
void account::deposit(double amount){
balance = balance + amount;
}
//----------------------------------------------------
//withdraw
void account::withdraw(double amount){
if(amount < balance ){
std::cout << "Debit amount exceeded account balance."
<< amount << endl;
}
else if(amount < 0){
std::cout <<"The withdrawel you've enter is defined as negative."
<< amount << endl;
}
else{
balance = balance - amount;
}
}
//----------------------------------------------------
//get balance
double account::get_balance() const{
return balance;
}
//----------------------------------------------------
//display
const std::string account::asstring() const{
ostringstream oss;
oss << "Account Num: " << m_acctnum <<
" Balance: " << m_balance;
return oss.str();
}
Test Program This is where I am having trouble in creating the program to do this. I think I need an array, maybe multidimensional array and access to the balance so I can perform deposits and withdrawals on??
#include <iostream>
#include <string>
#include "account.h"
void main(){
account::account[][2] = {
{44421, 20},
{55531, }
};
for (int row = 0; row < 2; ++row)
{
for (int col = 0; col < 2 ; ++col)
{
account::deposit[col]
}
}
}
You have multiple problems with your code.
//account number
int m_acctnum;
Your class member is an int.
const std:string get_acctnum() const{
return m_acctnum;
}
account::account(const std::string acctnum){
m_balance = 0;
m_acctnum = acctnum;
}
But your methods think it's a std::string. You cannot assign a std::string to an int. Similarly, you cannot convert an int directly to a std::string.
I'm sure your compiler reported all of these as errors. Did you understand your compiler's error messages? Here's a simple question, do you think that the following piece of code is valid:
int n="the rain in spain falls mainly in the plane";
Of course not, yet this is what you're trying to do, and, of course, the compiler can't be happy about it.
account::account[][2] = {
{44421, 20},
{55531, }
};
It's not clear what this is supposed to be, either. Each instance of an class is instantiated by either explicitly invoking the class's constructor, or using the class's default constructors; and then assigning it to a variable. Class instances are not instantiated by manually initializing private class members.
Your account class has a default constructor, and a constructor that takes one parameter. Your class does not have any constructors that take two parameters, so this is not a valid initialization.
Additionally, a class is constructed by giving the class's name, and the name of the class instance, and not by explicitly invoking the class's constructor.
account::deposit[col]
account::deposit() is a class method. It is not a static class function that can be invoked like that. Additionally, parameters to either a class method, or a static class functions are passed using parenthesis, not brackets.
Additionally, the deposit() method takes a parameter that's described as a "balance". It is not a "column number" type of a parameter.

Undefined Reference c++ lost

#include "assert.h"; // for some reason assert wouldn't work on my compiler without this
#include <iostream>
#include <string>
#include <limits> // This is helpful for inputting values. Otherwise, funny stuff happens
using namespace std;
class Product
{
public:
Product();
Product(string the_name, int the_price, int number_of);
string return_name();
void reduce_amount();
void print_data() const;
private:
string prod_name; // name of your product
int price_in_cents; // it's price in cents
int amount; // the number of the product that you have
};
Product::Product()
{
prod_name = "NULL_NAME: NEED DATA";
price_in_cents = 0;
}
Product::Product(string the_name, int the_price, int number_of)
{
assert(the_price>0);
assert(number_of>0);
assert(number_of<21);
assert(prod_name !="NULL_NAME: NEED DATA");
prod_name = the_name;
price_in_cents = the_price;
amount = number_of;
}
void Product::print_data() const
{
cout<<prod_name << endl;
cout<<"The price in cents is: " <<price_in_cents<< endl;
cout<< "Amount left: " << " " << amount << endl;
}
void Product::reduce_amount()
{
amount = amount -1;
}
string Product::return_name()
{
return prod_name;
}
class Vending_Machine
{
public:
Vending_Machine();
void empty_coins();
void print_vend_stats();
void add_product();
Product buy_product();
private:
int income_in_cents;
Product product1();
Product product2();
Product product3();
Product product4();
Product product5();
};
void Vending_Machine::empty_coins()
{
cout << "The total amount of money earned today is " << income_in_cents << " cents" << endl;
income_in_cents = 0;
cout << "All the coins have been withdrawn. The balance is now zero." << endl;
}
void Vending_Machine::print_vend_stats()
{
cout<< "Total income thus far: " << income_in_cents << endl;
if (product1().return_name() != "NULL_NAME: NEED DATA")
{
//stuff happens
}
}
int main()
{
return 0;
}
So, I'm not sure if I did all the identation correctly, but I'm having a problem with the boolean statement in vending machine print_vend_stats() function. It's saying I am making an undefined fereence to product1(). What does this mean?
When you declare
Product product1();
you declare a member function, the parentheses is what makes it a function.
If you drop the parentheses
Product product1;
you declare a member variable, an actual instance of the Product class.
Another example, you wouldn't write e.g.
int income_in_cents();
do declare income_in_cents as a variable, now would you?
It doesn't matter if the type is a primitive type like int, or a class like Product, Member variables are declared like normal variables like you do anywhere else.