🧩 C++ programming tutorial for beginners · Class 11 · Lesson 6 of 18 · Free
C++ Projects – Build a Multi-Class Kirana Shop Billing App
⏱ About 45 minutes · ▶ run real C++ here — nothing to install · no sign-in
In this lesson you will
- Plan a program by turning nouns into classes and verbs into member functions
- Make classes work together, with one class holding objects of another
- Run a command menu and split a project into header and source files
Sharma Uncle's kirana shop writes bills by hand, and stock runs out without warning. In Lessons 1 to 5 you learned classes, inheritance, polymorphism and the STL. Now you join them into one real program: a billing app with products, a shop and a bill that talk to each other.
How do I plan a C++ project with classes?
Before coding, write the problem in one or two sentences. "The shop has products. A customer's bill adds products and reduces the stock."
The nouns become classes: Product, Shop, Bill. The verbs become member functions: sell, find, add. This simple trick gives you a first design.
Build and test one class at a time. Here is Product alone, with a quick test in main. Small tests catch bugs early, when they are easy to fix.
Try it: call sell(9) on rice and see the refusal.
Rice 1kg: Rs 52, 10 in stock Sell 3: done Sell 8: refused Rice 1kg: Rs 52, 7 in stock
How do classes work together in C++?
A Shop has many products, so Shop holds a vector<Product>. One class containing objects of another is called composition. It is the "has-a" partner of inheritance's "is-a" (Lesson 3).
find(code) returns a pointer to the product inside the shop, so the caller can change the real stock. If no product has that code, it returns nullptr, which means "points to nothing".
Always check for nullptr before using the pointer. Using a null pointer crashes the program.
Try it: look up code 999 and see the "not found" branch.
Code 999 not found Reorder: Rice 1kg (3) Reorder: Dal 1kg (4)
💡 Don't keep such a pointer for long. If the vector grows later, it may move its items and the old pointer goes stale.
How do I make a bill class with a total and discount in C++?
A Bill keeps a list of lines. Each line has a name, a quantity and a price. A small struct is perfect for that.
total() adds up the lines. The shop gives 5% off when the bill is Rs 500 or more. Keeping that rule inside Bill means it lives in one place only.
print() uses total(), so the printed bill and the real total can never disagree.
Try it: remove the oil line so the bill drops below Rs 500, and see the discount vanish.
4 x Rice 1kg = Rs 208 2 x Oil 1L = Rs 290 3 x Biscuits = Rs 30 Discount: Rs 26.4 To pay: Rs 501.6
How do I make a menu-driven console program in C++?
Now everything meets. main reads a command again and again: add CODE QTY, total or quit. Each command calls the right object.
The loop condition prints a prompt, reads a word and stops on "quit" (or when input runs out). Bill::add changes the shop's stock through a reference, so stock stays correct.
This is the biggest program so far, but each class is small and does one job. That is the point of splitting a program into classes.
Try it: edit the inputs. Change "add 102 9" to "add 102 4", or add a line "add 555 1" (before quit) for a missing product.
Command (add CODE QTY / total / quit): add 101 2 2 x Rice 1kg = Rs 104 Command (add CODE QTY / total / quit): add 103 5 5 x Biscuits = Rs 50 Command (add CODE QTY / total / quit): add 102 9 Only 4 left of Dal 1kg Command (add CODE QTY / total / quit): total Total: Rs 154 Command (add CODE QTY / total / quit): quit Thank you!
How do I split a C++ project into header and .cpp files?
Real projects keep each class in two files. The header (product.h) says what the class has. The source file (product.cpp) holds the function bodies.
Other files write #include "product.h" to use the class. #pragma once stops the header from being pasted in twice.
Product::show() means "the show function that belongs to Product", written outside the class. All three files are compiled together into one program.
Try it: add a member function to product.h, write its body in product.cpp, and call it from main.
#pragma once
#include <string>
class Product {
std::string name;
double price;
int stock;
public:
Product(std::string n, double p, int s);
bool sell(int qty);
void show() const;
};
#include <iostream>
#include "product.h"
using namespace std;
Product::Product(string n, double p, int s) : name(n), price(p), stock(s) {}
bool Product::sell(int qty) {
if (qty <= 0 || qty > stock) return false;
stock -= qty;
return true;
}
void Product::show() const {
cout << name << ": Rs " << price << ", " << stock << " in stock\n";
}
Tea 250g: Rs 120, 6 in stock Tea 250g: Rs 120, 4 in stock
💡 Headers usually write std::string instead of using namespace std, so every file that includes them does not get the shortcut forced on it.
🤔 Predict the output
Think like C++: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
#include <iostream>
#include <vector>
using namespace std;
class Classroom {
vector<int> marks;
public:
void add(int m) {
if (m >= 0 && m <= 100) marks.push_back(m);
}
int count() const { return marks.size(); }
};
int main() {
Classroom c;
c.add(90);
c.add(120);
c.add(-5);
c.add(40);
cout << c.count() << "\n";
return 0;
}
Question 2. What does this print?
#include <iostream>
#include <vector>
using namespace std;
struct Item { int code; int stock; };
class Shop {
vector<Item> items = {{1, 10}, {2, 5}};
public:
Item* find(int c) {
for (Item& i : items) if (i.code == c) return &i;
return nullptr;
}
};
int main() {
Shop s;
Item* p = s.find(2);
p->stock -= 3;
cout << s.find(2)->stock << "\n";
return 0;
}
Question 3. What does this print?
#include <iostream>
#include <vector>
using namespace std;
struct Item { int stock; };
class Shop {
vector<Item> items = {{10}};
public:
Item get(int i) const { return items[i]; }
};
int main() {
Shop s;
Item x = s.get(0);
x.stock = 0;
cout << s.get(0).stock << "\n";
return 0;
}
🧩 Your challenge
Build a tiny tiffin service. Class Dish has a name and a price. Class Order holds a vector of (Dish, quantity) lines, with add(Dish, qty) and a total() that adds Rs 30 delivery when the food costs less than Rs 200. print() shows each line and the total. For 2 idli (Rs 40) and 1 dosa (Rs 70): 2 x Idli = Rs 80 1 x Dosa = Rs 70 Delivery: Rs 30 Total: Rs 180
2 x Idli = Rs 80 1 x Dosa = Rs 70 Delivery: Rs 30 Total: Rs 180
⚠️ Common mistakes (and how to fix them)
❌ Wrong
#include <iostream>
#include <vector>
using namespace std;
class Shop {
vector<Product> items;
public:
int size() const { return items.size(); }
};
struct Product {
int code;
};
int main() {
Shop s;
cout << s.size() << "\n";
return 0;
}
Line 6: error: use of undeclared identifier 'Product'
✅ Right
#include <iostream>
#include <vector>
using namespace std;
struct Product {
int code;
};
class Shop {
vector<Product> items;
public:
int size() const { return items.size(); }
};
int main() {
Shop s;
cout << s.size() << "\n";
return 0;
}
C++ reads a file from top to bottom. A class must be declared before another class uses it, so put Product above Shop (or in a header included at the top).
❌ Wrong
#include <iostream>
#include <vector>
using namespace std;
struct Item { int stock; };
class Shop {
vector<Item> items = {{10}};
public:
Item get(int i) { return items[i]; }
int stockOf(int i) const { return items[i].stock; }
};
int main() {
Shop s;
s.get(0).stock -= 4;
cout << s.stockOf(0) << "\n";
return 0;
}
Line 16: error: expression is not assignable
✅ Right
#include <iostream>
#include <vector>
using namespace std;
struct Item { int stock; };
class Shop {
vector<Item> items = {{10}};
public:
Item& get(int i) { return items[i]; }
int stockOf(int i) const { return items[i].stock; }
};
int main() {
Shop s;
s.get(0).stock -= 4;
cout << s.stockOf(0) << "\n";
return 0;
}
Returning Item gives back a temporary copy, so changing it would be lost at once. The compiler refuses. Return Item& to hand out the real item.
❌ Wrong
#include <iostream>
using namespace std;
class Bill {
double total = 250;
public:
double getTotal() { return total; }
};
void printBill(const Bill& b) {
cout << "Total: " << b.getTotal() << "\n";
}
int main() {
Bill b;
printBill(b);
return 0;
}
Line 11: error: 'this' argument to member function 'getTotal' has type 'const Bill', but function is not marked const
✅ Right
#include <iostream>
using namespace std;
class Bill {
double total = 250;
public:
double getTotal() const { return total; }
};
void printBill(const Bill& b) {
cout << "Total: " << b.getTotal() << "\n";
}
int main() {
Bill b;
printBill(b);
return 0;
}
Through a const reference you may call only const member functions. Mark getters that only read data as const.
📌 Remember
- Plan first: nouns become classes, verbs become member functions.
- Build and test one class at a time before joining them.
- Composition (has-a): a Shop holds a vector of Products.
- Return a pointer or reference to change the real object; check pointers for nullptr.
- Big projects split each class into a .h header and a .cpp source file.
Finished the quiz and the challenge?
Questions students ask
What are good C++ project ideas for beginners?
Small console apps with real rules work well: shop billing, a library register, a quiz with scores, a railway seat booker or a report card maker. Start with one class, then add more once it works.
What is the difference between composition and inheritance in C++?
Inheritance is for is-a: a Teacher is a Person. Composition is for has-a: a Shop has Products, so it keeps them as a member. When unsure, prefer composition; it keeps classes simpler.
Why do C++ projects use header files?
A header lets many .cpp files share one class declaration without copying it. Each .cpp file is compiled on its own, which also makes big projects rebuild faster after a small change.
What is nullptr in C++?
nullptr is a pointer value that means "points to nothing". Functions use it to say "not found". Using a nullptr as if it pointed to an object crashes the program, so check it first.