Goodday ,

I have created an object in a private class and want to access that object in that classes function. It would seem that it is not working.

When I answer yes to manufacturer the output is missing name, email and product supplier. If I say no then product supplier is still missing.

Enter Product_Name:
test product
Enter Product_Price:
333
Enter Supplier_Name:
shops
Enter Supplier_Email:
email.com
Is the supplier a manufacturer (Y/N) :
y

Vendor name:
Vendor email:
Product Name: test product
Product Price: R333
Product Supplier:
Press <RETURN> to close this window...

Here is my code as I am not sure why I am getting this issue.

Thanks for the help

Product.h

Qt Code:
  1. #ifndef PRODUCT_H
  2. #define PRODUCT_H
  3.  
  4. #include "Vendor.h"
  5. #include <QString>
  6.  
  7. class Product {
  8.  
  9. private:
  10. QString m_Name;
  11. double m_Price;
  12. //A vendor object
  13. Vendor m_Supplier;
  14.  
  15. public:
  16. Product (QString name, double price);
  17. void setSupplier(QString name, QString email, bool isManufacturer);
  18. QString getManufacturerName();
  19. QString toString(bool supplierDetails);
  20.  
  21. };
  22. #endif // PRODUCT_H
To copy to clipboard, switch view to plain text mode 

Vendor.h

Qt Code:
  1. #ifndef VENDOR_H
  2. #define VENDOR_H
  3.  
  4. #include <QString>
  5.  
  6. class Vendor {
  7.  
  8. private:
  9. QString m_Name;
  10. QString m_Email;
  11. bool m_IsManufacturer;
  12.  
  13. public:
  14. Vendor();
  15. void setDetails(QString name, QString email, bool isManufacturer);
  16. bool isManufacturer();
  17. QString getName();
  18. QString toString();
  19.  
  20. };
  21. #endif // VENDOR_H
To copy to clipboard, switch view to plain text mode 

Main.cpp

Qt Code:
  1. #include <QCoreApplication>
  2. #include "Product.h"
  3. #include"Vendor.h"
  4. #include <QString>
  5. #include <QDebug>
  6. #include <QTextStream>
  7.  
  8. using namespace std;
  9.  
  10. QTextStream cout(stdout);
  11. QTextStream cin(stdin);
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. QCoreApplication a(argc, argv);
  16.  
  17. QString product_Name;
  18. QString product_Price;
  19. double productPrice;
  20. QString supplier_Name;
  21. QString supplier_Email;
  22. QString supplier_IsManufacturer;
  23. bool supplierIsManufacturer;
  24.  
  25. cout << "Enter Product_Name:" << endl;
  26. product_Name = cin.readLine();
  27. cout << "Enter Product_Price:" << endl;
  28. product_Price = cin.readLine();
  29. cout << "Enter Supplier_Name:" << endl;
  30. supplier_Name = cin.readLine();
  31. cout << "Enter Supplier_Email:" << endl;
  32. supplier_Email = cin.readLine();
  33. cout <<"Is the supplier a manufacturer (Y/N) : " << endl;
  34. supplier_IsManufacturer = cin.readLine();
  35.  
  36. productPrice = product_Price.toDouble();
  37.  
  38. if(supplier_IsManufacturer.at(0).toLower() == 'y') {
  39. supplierIsManufacturer = true;
  40. }
  41.  
  42. Vendor m_Supplier;
  43. Product product (product_Name, productPrice);
  44. m_Supplier.setDetails(supplier_Name, supplier_Email, supplierIsManufacturer);
  45.  
  46. cout << product.toString(supplierIsManufacturer) << endl;
  47.  
  48. return 0;
  49.  
  50. }
To copy to clipboard, switch view to plain text mode 

product.cpp

Qt Code:
  1. #include "Product.h"
  2. #include<QString>
  3.  
  4. Product::Product (QString name, double price) {
  5.  
  6. m_Name = name;
  7. m_Price = price;
  8.  
  9. }
  10.  
  11. void Product::setSupplier(QString name, QString email, bool isManufacturer){
  12.  
  13. m_Supplier.setDetails(name, email, isManufacturer);
  14. }
  15.  
  16. QString Product::getManufacturerName() {
  17.  
  18. if(m_Supplier.isManufacturer()) {
  19. return m_Supplier.getName();
  20. }
  21. else {
  22. return "Unknown";
  23. }
  24. }
  25.  
  26. QString Product::toString(bool supplierDetails){
  27.  
  28. QString dataout;
  29.  
  30. dataout = "\nProduct Name:\t\t" + m_Name + "\nProduct Price:\t\tR" + QString::number(m_Price) + "\nProduct Supplier:\t" + getManufacturerName();
  31.  
  32. if(supplierDetails) {
  33. return m_Supplier.toString() + dataout;
  34. }
  35. else{
  36. return dataout;
  37. }
  38.  
  39.  
  40. }
To copy to clipboard, switch view to plain text mode 


Vendor.cpp

Qt Code:
  1. #include"Vendor.h"
  2. #include<QString>
  3.  
  4.  
  5. Vendor::Vendor() {}
  6.  
  7. void Vendor::setDetails(QString name, QString email, bool isManufacturer){
  8.  
  9. m_Name = name;
  10. m_Email = email;
  11. m_IsManufacturer = isManufacturer;
  12.  
  13. }
  14.  
  15. bool Vendor::isManufacturer (){
  16. return m_IsManufacturer;
  17. }
  18.  
  19. QString Vendor::getName()
  20. {
  21.  
  22. return m_Name;
  23.  
  24. }
  25.  
  26. QString Vendor::toString(){
  27.  
  28. QString dataout;
  29.  
  30. if( m_IsManufacturer ) {
  31. return dataout = "\nVendor name:\t\t" + m_Name + "\nVendor email:\t\t" + m_Email ;
  32.  
  33. }
  34. }
To copy to clipboard, switch view to plain text mode