My very first class.

Hi guys,
I have created (as partly help to someone question here, in other part to try building it as exercise) and I have few questions.

I am not sure about few (at least) things:
1. How to create multiple instances of an object in my code

should I create Product1 Product2 etc? How would that be done if user will decide how many products there will be? maybe 1 maybe 100??

2. how to delete Product as added entry by user?

3. my edit function is this how i should edit product, or is a better way?

4. Product overload - If I have similar functions, i use product overloading, however, how should i deal with, edit, view or add product? I want to add product 1, edit product 1 or view product 1.

what do you do with such situation????

void addProduct(int productID);
void editProduct(int productID);
void viewProduct(int productID);



5. Based on answer to first question i think my loop will have be changed to reflect correct way of doing it :D so I expect that I will have more questions.

my code is available here: https://github.com/xxvms/Products-Add-View-Edit

and below listed


thanks :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

//Product add view modify and delte
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include <string>
#include "Product.h"




int main()
{
	int id = 0;
	int choice = 9;

	Product  Product;
	while (true)
	{
		std::cout << "Please make your selection: " << std::endl;
		std::cout << "Press 1 to Add Product." << std::endl;
		std::cout << "Press 2 to Edit Product." << std::endl;
		std::cout << "Press 3 to View Product." << std::endl;
		std::cout << "Press 4 to Delete Product." << std::endl;
		std::cout << "Press 0 to Quit." << std::endl;

		std::cin >> choice;

		if (choice == 1)
		{
			std::cout << "Plese type product ID to Add" << std::endl;
			std::cin >> id;
			Product.addProduct(id);
		}
		else if (choice == 2)
 		{
			std::cout << "Plese type product ID to edit" << std::endl;
			std::cin >> id;
			Product.editProduct(id);
		}
		else if (choice == 3)
		{
			std::cout << "Plese type product ID to view" << std::endl;
			std::cin >> id;
			Product.viewProduct(id);

		}
		else if (choice == 4)
		{
			//Product.~Product;
		}
		else if (choice == 0)
		{
			break;
		}

	}
	system("pause");
	return 0;
}

 


Product.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include <string>

class Product
{
public:
	Product();
	void addProduct(int);
	void viewProduct(char);
	void editProduct(int);

	~Product();

private:
	char name;
	int quantity;
	double price;
	int productID;
	std::string description;
};


Product.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "stdafx.h"
#include "Product.h"
#include <iostream>
#include <string>


Product::Product()
{
	
}

void Product::addProduct(int productID1)
{
	system("cls");
	productID = productID1;
	std::cout << "Please type new Name: ";
	std::cin >> name;
	std::cout << "Please type new quantity: ";
	std::cin >> quantity;
	std::cout << "Please type new price: ";
	std::cin >> price;
	std::cout << "Please type new description: ";
	std::cin >> description;
}
void Product::viewProduct(char name_)
{
	system("cls");
	std::cout << "Product ID: " << productID << std::endl;
	std::cout << "Product Name: " << name << std::endl;
	std::cout << "Quantity: " << quantity << std::endl;
	std::cout << "Price: " << price << std::endl;
	std::cout << "Product Description: " << description << std::endl;
}
void Product::editProduct(int productID_)
{
	productID = productID_;
	system("cls");
	std::cout <<"Current name is " << name << " Please type new Name: ";
	std::cin >> name;
	std::cout << "Current quantity is " << quantity << " Please type new quantity: ";
	std::cin >> quantity;
	std::cout << "Current price is " << price << " Please type new price: ";
	std::cin >> price;
	std::cout << "Current description is " << description << std::endl;
	std::cout << "Please type new description: ";
	std::cin >> description;
}

Product::~Product()
{

}
How to create multiple instances of an object in my code

by declaring variables of the object-type as need (and use RAII through the Product ctors)

how to delete Product as added entry by user

this is shown as a test case on the other link you allude to: http://www.cplusplus.com/forum/beginner/213368/

However the mode of deletion is also container dependent and may I suggest, particularly as you're starting from scratch, to use std::vector<Product> as the container of choice whereby you can use the erase-remove_if idiom to erase elements from the vector:
https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
using a vector the addProduct() becomes as simple as calling the Product ctor and then push_back(std::move()) the created Product object into the vector
editProduct() - you could continue to use std::find_if on the vector to find the product object and then, if found, edit it
viewProduct() - here you may consider overloading the stream insertion operator >> and then range-looping the vector and printing the vector elements via the range loop
Hi Gunnerfunner

thanks for that :)

I am not sure what that means .... and use RAII through the Product ctors (google was not sure either)

I was thinking about using vector for this to hold data nicely organised, because I was editing someone else post I didn't want to change this to vector.
However, I will try now :)

(but first read, re read... (while (true) ) then do it ;)
I am not sure what that means .... and use RAII through the Product ctors 

in this specific context it means encapsulate the resources needed to construct Product objects directly through the Product ctor. so for e.g. suppose a Product object needs two resources for it's construction: a name (std::string) and a number (int), we'd do: Product p{name, number}; thereby resources are acquired and the object is initialized at one go and, as this object is created on the stack, the acquired resources are released when the object goes out of scope
Topic archived. No new replies allowed.