Cpp
Cpp
class University {
private:
Department department;
public:
void display() {
department.display();
std::cout << "University" << std::endl;
}
};
#include <iostream>
const int N = 9;
int main() {
int board[N][N] = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};
if (solveSudoku(board)) {
printBoard(board);
} else {
std::cout << "No solution exists" << std::endl;
}
return 0;
}
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
#include <vector>
#include <iostream>
int main() {
std::vector<int> numbers = {1, 2, 3};
numbers.push_back(4);
for (auto num : numbers) {
std::cout << num << " ";
}
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> numbers = {4, 2, 9, 6};
std::sort(numbers.begin(), numbers.end());
for (auto num : numbers) {
std::cout << num << " ";
}
return 0;
}
#include <string>
#include <iostream>
int main() {
std::string greeting = "Hello";
greeting += ", World!";
std::cout << greeting << std::endl;
return 0;
}
#include <map>
#include <iostream>
int main() {
std::map<std::string, int> ages = {{"Alice", 25}, {"Bob", 30}};
ages["Charlie"] = 35;
for (auto& pair : ages) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Backtracking Algorithm
1. Start with an empty board.
2. Try to place a queen in each column of the current row.
3. Check if the placement is safe (no queen in the same row, column, or diagonals).
4. If safe, recursively try to place queens in the next row.
5. If unable to place a queen in any column, backtrack (remove the queen from the current
row and try another column).
#include <iostream>
#include <vector>
board[i][col] = 0; // Backtrack
}
}
return false;
}
int main() {
int n = 4;
std::vector<std::vector<int>> board(n, std::vector<int>(n, 0));
if (solveNQueens(board, 0, n)) {
printBoard(board, n);
} else {
std::cout << "Solution does not exist" << std::endl;
}
return 0;
}
This code solves the N-Queens problem for a 4x4 board but can be easily extended for any size
n.
⁂
also lets go with dependy indjection,and what are
the other good conecpt of cpp give me it all
Let's cover dependency injection and other advanced C++ concepts.
class Engine {
public:
virtual void start() = 0;
};
class Car {
private:
Engine* engine;
public:
Car(Engine* e) : engine(e) {}
void drive() {
engine->start();
std::cout << "Car is moving." << std::endl;
}
};
int main() {
V8Engine engine;
Car car(&engine);
car.drive();
return 0;
}
In this example, Car depends on Engine, but the specific type of engine is injected at runtime.
#include <memory>
int main() {
std::unique_ptr<int> ptr(new int(10));
// ptr is automatically deleted when it goes out of scope
return 0;
}
2. Concepts (C++20)
Purpose: Constraints for template parameters to improve code readability and
maintainability.
Example:
#include <concepts>
int main() {
add(1, 2); // Works
add(1.0, 2.0); // Works
return 0;
}
3. Lambda Expressions
Purpose: Anonymous functions that can be defined inline.
Example:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3};
std::for_each(numbers.begin(), numbers.end(), [](int i) {
std::cout << i << " ";
});
return 0;
}
4. Smart Pointers
Purpose: Manage memory automatically to prevent leaks.
Types:
std::unique_ptr: Exclusive ownership.
std::shared_ptr: Shared ownership.
std::weak_ptr: Weak reference to std::shared_ptr.
#include <memory>
int main() {
std::shared_ptr<int> ptr(new int(10));
// ptr is automatically deleted when all shared_ptr instances go out of scope
return 0;
}
5. Move Semantics
Purpose: Efficiently transfer ownership of objects.
Example:
class MyClass {
public:
MyClass() = default;
MyClass(const MyClass&) = delete; // Disable copying
MyClass(MyClass&&) = default; // Enable moving
};
int main() {
MyClass obj1;
MyClass obj2 = std::move(obj1); // Transfer ownership
return 0;
}
6. Const Correctness
Purpose: Ensure variables are not modified unnecessarily.
Example:
void print(const std::string& str) {
std::cout << str << std::endl;
}
int main() {
const std::string str = "Hello";
print(str); // str remains unchanged
return 0;
}
#include <vector>
#include <iostream>
int main() {
std::vector<int> numbers = {1, 2, 3};
for (auto& num : numbers) {
std::cout << num << " ";
}
return 0;
}
These concepts help write cleaner, more efficient, and maintainable C++ code.
⁂
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int createZombie() {
if (rand() % 67 < 10)
return 11;
else
return rand() % 10 + 1;
}
int main() {
srand(time(NULL));
char enter;
int playerAlive = true;
int playerSkill = 9;
int playerScore = 1;
string playerName;
int zombieCount = 0;
int zombiesKilled = 0;
cout << "Welcome to Zombie War." << endl << "Press [ENTER] to start.";
cin.get();
cout << "Please enter your name: ";
cin >> playerName;
cout << "How many zombies do you wish to fight? ";
cin >> zombieCount;
cout << "Get ready to fight for your life, " << playerName << "!" << endl;
if (zombiesKilled == zombieCount) {
cout << "You have survived the onslaught!" << endl;
} else {
cout << "You did not survive the zombie war." << endl;
}
cout << "Zombies killed: " << zombiesKilled << endl;
cout << "Final score: " << playerScore << endl << endl;
return 0;
}
2. Snake Game
A simple implementation of the classic Snake game using console output.
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
void setup() {
srand(time(0));
gameover = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}
void draw() {
system("cls");
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
void input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
gameover = true;
break;
}
}
}
void logic() {
int prevX = tailX[^3_0];
int prevY = tailY[^3_0];
for (int i = 1; i < nTail; i++) {
int prev2X = tailX[i];
int prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
if (dir == LEFT) {
x--;
} else if (dir == RIGHT) {
x++;
} else if (dir == UP) {
y--;
} else if (dir == DOWN) {
y++;
}
if (x >= width) x = 0;
else if (x < 0) x = width - 1;
if (y >= height) y = 0;
else if (y < 0) y = height - 1;
int main() {
bool gameover = false;
setup();
while (!gameover) {
draw();
input();
logic();
}
return 0;
}
3. Tic-Tac-Toe Game
A simple console-based Tic-Tac-Toe game for two players.
#include <iostream>
using namespace std;
char board[^3_3][^3_3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
void drawBoard() {
cout << " " << board[^3_0][^3_0] << " | " << board[^3_0][^3_1] << " | " << board[^3_0
cout << "---+---+---" << endl;
cout << " " << board[^3_1][^3_0] << " | " << board[^3_1][^3_1] << " | " << board[^3_1
cout << "---+---+---" << endl;
cout << " " << board[^3_2][^3_0] << " | " << board[^3_2][^3_1] << " | " << board[^3_2
}
bool checkWin(char player) {
for (int i = 0; i < 3; i++) {
if (board[i][^3_0] == player && board[i][^3_1] == player && board[i][^3_2] == pla
return true;
if (board[^3_0][i] == player && board[^3_1][i] == player && board[^3_2][i] == pla
return true;
}
if ((board[^3_0][^3_0] == player && board[^3_1][^3_1] == player && board[^3_2][^3_2]
(board[^3_0][^3_2] == player && board[^3_1][^3_1] == player && board[^3_2][^3_0]
return true;
return false;
}
int main() {
char player = 'X';
int choice;
bool gameOver = false;
while (!gameOver) {
drawBoard();
cout << "Player " << player << ", enter your choice (1-9): ";
cin >> choice;
return 0;
}
#include <iostream>
#include <ctime>
#include <cstdlib>
int main() {
srand(time(0));
int playerScore = 0, computerScore = 0;
while (true) {
cout << "Enter your choice (1 for Rock, 2 for Paper, 3 for Scissors): ";
int playerChoice;
cin >> playerChoice;
if (playerChoice == computerChoice) {
cout << "It's a tie!" << endl;
} else if ((playerChoice == 1 && computerChoice == 3) ||
(playerChoice == 2 && computerChoice == 1) ||
(playerChoice == 3 && computerChoice == 2)) {
cout << "You win this round!" << endl;
playerScore++;
} else {
cout << "Computer wins this round!" << endl;
computerScore++;
}
cout << "Your score: " << playerScore << ", Computer score: " << computerScore <<
char playAgain;
cout << "Play again? (y/n): ";
cin >> playAgain;
return 0;
}
These examples cover different aspects of game development in C++, from simple text-based
games to more interactive console games.
⁂