0% found this document useful (0 votes)
46 views7 pages

Lab 12 22235103361

The document defines a Player_Basic class with health and damage attributes. It then defines Player1, Player2, and Player3 classes that inherit from Player_Basic and initialize players of each class. The health of each player is printed initially, players take damage and heal, and final health is printed.

Uploaded by

lucifesk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views7 pages

Lab 12 22235103361

The document defines a Player_Basic class with health and damage attributes. It then defines Player1, Player2, and Player3 classes that inherit from Player_Basic and initialize players of each class. The health of each player is printed initially, players take damage and heal, and final health is printed.

Uploaded by

lucifesk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

‭ask 5: First of I’m sorry cause I don’t know the mechanism of CPP.


T
This is why I’m writing this program in python. Forgive me for this. In‬

this code my scenario is I have a game and there is three player in it‬

there basic characteristic is Health and getting_damage. And if I‬

started to make health and getting damage it will make the code bulky‬

and this will become my headache so I made a class for it called‬

Player_Basic and inherit them into the other three player’s classes.‬

Finally show their First health and after getting damage health.‬

class‬‭
‭ Player_Basic‬
:‬

def‬‭
‭ __init__‬
(‭
‭s‬elf‬
,‬‭
‭ name‬
,‬‭
‭ max_health‬
):‬

self‬
‭ .‭
‭n‬ame‬‭
=‬‭
name‬
self‬
‭ .‭
‭m‬ax_health‬‭
=‬‭
max_health‬
self‬
‭ .‭
‭h‬ealth‬‭
=‬‭
max_health‬
def‬‭
‭ take_damage‬
(‭
‭s‬elf‬
,‬‭
‭ damage‬
):‬

self‬
‭ .‭
‭h‬ealth‬‭
-=‬‭
damage‬
if‬‭
‭ self‬
.‬
‭ health‬‭
‭ <‬‭
0‬:‬

self‬
‭ .‭
‭h‬ealth‬‭
=‬‭
0‬
def‬‭
‭ heal‬
(‭
‭s‬elf‬
,‬‭
‭ amount‬
):‬

self‬
‭ .‭
‭h‬ealth‬‭
+=‬‭
amount‬
if‬‭
‭ self‬
.‬
‭ health‬‭
‭ >‬‭
self‬
.‬
‭ max_health‬
‭ :‬

self‬
‭ .‭
‭h‬ealth‬‭
=‬‭
self‬
.‭
‭m‬ax_health‬
def‬‭
‭ get_health‬
(‬
‭ self‬
‭ ):‬

return‬‭
‭ self‬
.‭
‭h‬ealth‬
def‬‭
‭ is_alive‬
(‭
‭s‬elf‬
):‬

return‬‭
‭ self‬
.‭
‭h‬ealth‬‭
>‬‭
0‬
class‬‭
‭ Player1‬
(‬
‭ Player_Basic‬
‭ ):‬

def‬‭
‭ __init__‬
(‭
‭s‬elf‬
,‬‭
‭ name‬
):‬

super‬
‭ ().‬
‭ __init__‬
‭ (‬
‭ name‬
‭ ,‬‭
‭ max_health‬
=‬
‭ 100‬
‭ )‬

class‬‭
‭ Player2‬
(‬
‭ Player_Basic‬
‭ ):‬

def‬‭
‭ __init__‬
(‭
‭s‬elf‬
,‬‭
‭ name‬
):‬

super‬
‭ ().‬
‭ __init__‬
‭ (‬
‭ name‬
‭ ,‬‭
‭ max_health‬
=‬
‭ 120‬
‭ )‬

class‬‭
‭ Player3‬
(‬
‭ Player_Basic‬
‭ ):‬

def‬‭
‭ __init__‬
(‭
‭s‬elf‬
,‬‭
‭ name‬
):‬

super‬
‭ ().‬
‭ __init__‬
‭ (‬
‭ name‬
‭ ,‬‭
‭ max_health‬
=‬
‭ 100‬
‭ )‬

# Example usage:‬

player1‬‭
‭ =‬‭
Player1‬
(‬
‭ "Player 1"‬
‭ )‬

player2‬‭
‭ =‬‭
Player2‬
(‬
‭ "Player 2"‬
‭ )‬

player3‬‭
‭ =‬‭
Player3‬
(‬
‭ "Player 3"‬
‭ )‬

print‬
‭ (‭
‭f‬‭
"
‬‬{‭
‭p‬layer1‬
.‭
‭ n
‬ame‬
}‬
‭ 's health:‬‭
‭ {‭
p‬layer1‬
.‬
‭ get_health‬
‭ ()‬
‭ }‬
‭ "‭
‭)‬‬
print‬
‭ (‭
‭f‬‭
"
‬‬{‭
‭p‬layer2‬
.‭
‭ n
‬ame‬
}‬
‭ 's health:‬‭
‭ {‭
p‬layer2‬
.‬
‭ get_health‬
‭ ()‬
‭ }‬
‭ "‭
‭)‬‬
print‬
‭ (‭
‭f‬‭
"
‬‬{‭
‭p‬layer3‬
.‭
‭ n
‬ame‬
}‬
‭ 's health:‬‭
‭ {‭
p‬layer3‬
.‬
‭ get_health‬
‭ ()‬
‭ }‬
‭ "‭
‭)‬‬
player1‬
‭ .‬
‭ take_damage‬
‭ (‭
‭2‬0‬
)‬

player1‬
‭ .‬
‭ heal‬
‭ (‭
‭2‬0‬
)‬

player2‬
‭ .‬
‭ take_damage‬
‭ (‭
‭2‬0‬
)‬

player2‬
‭ .‬
‭ heal‬
‭ (‭
‭3‬0‬
)‬

player2‬
‭ .‬
‭ heal‬
‭ (‭
‭0‬‬
)‬

player2‬
‭ .‬
‭ take_damage‬
‭ (‭
‭1‬00‬
)‬

print‬
‭ (‭
‭f‬‭
"
‬‬{‭
‭p‬layer1‬
.‭
‭ n
‬ame‬
}‬
‭ 's health:‬‭
‭ {‭
p‬layer1‬
.‬
‭ get_health‬
‭ ()‬
‭ }‬
‭ "‭
‭)‬‬
print‬
‭ (‭
‭f‬‭
"
‬‬{‭
‭p‬layer2‬
.‭
‭ n
‬ame‬
}‬
‭ 's health:‬‭
‭ {‭
p‬layer2‬
.‬
‭ get_health‬
‭ ()‬
‭ }‬
‭ "‭
‭)‬‬
if‬‭
‭ player3‬
.‬
‭ is_alive‬
‭ ():‬

print‬
‭ (‬
‭ f‬
‭ "‬
‭ {‭
‭p‬layer3‬
.‬
‭ name‬
‭ }‬‭
‭ is alive."‬
)‬

else‬
‭ :‬

print‬
‭ (‬
‭ f‬
‭ "‬
‭ {‭
‭p‬layer3‬
.‬
‭ name‬
‭ }‬‭
‭ is defeated."‬
)‬

Here is the Output‬


And I’m sorry for that again.‬


Task-2 {I took Help for this from Chat GPT.}‬



‭include <iostream>‬
#
#include <string>‬

using namespace std;‬


‭lass Player‬
c
{‬

public:‬

string name;‬

int age;‬

string country;‬

Player(const string& n, int a, const string& c) : name(n), age(a),‬



country(c)‬

{‬

}‬

};‬

‭lass Batsman : public Player‬


c
{‬

public:‬

float battingAverage;‬

int totalRuns;‬

int highestScore;‬

‭atsman(const string& n, int a, const string& c, float avg, int‬


B
runs, int highest) : Player(n, a, c), battingAverage(avg),‬

totalRuns(runs), highestScore(highest)‬

{‬

}‬

};‬

‭lass Bowler : public Player {‬


c
public:‬

float bowlingAverage;‬

int wicketsTaken;‬

string bowlingStyle;‬

‭owler(const string& n, int a, const string& c, float avg, int‬


B
wickets, const string& style) : Player(n, a, c), bowlingAverage(avg),‬

wicketsTaken(wickets), bowlingStyle(style)‬

{‬

}‬

};‬

‭lass SpecialistBatsman : public Batsman‬


c
{‬

public:‬

float strikeRate;‬

int numberOfCenturies;‬

‭pecialistBatsman(const string& n, int a, const string& c, float‬


S
avg, int runs, int highest, float rate, int centuries):Batsman(n, a, c,‬

avg, runs, highest), strikeRate(rate), numberOfCenturies(centuries)‬

{‬

}‬

};‬

‭nt main()‬
i
{‬

Player player1("Joe", 28, "England");‬

Batsman batsman1("Virat", 32, "India", 52.63, 9876, 183);‬

Bowler bowler1("James", 30, "Australia", 25.48, 264, "Fast");‬

SpecialistBatsman specialistBatsman1("Steve", 35, "Australia",‬

49.75, 11500, 164, 87.2, 30);‬

‭out << player1.name << " is a player from " << player1.country <<‬
c
"." << endl;‬

cout << batsman1.name << " is a batsman with a highest score of "‬

<< batsman1.highestScore << "." << endl;‬

cout << bowler1.name << " is a bowler with a bowling style of " <<‬

bowler1.bowlingStyle << "." << endl;‬

cout << specialistBatsman1.name << " is a specialist batsman with "‬

<< specialistBatsman1.numberOfCenturies << " centuries." << endl;‬

return 0;‬

}‬

Task-3 {I took help form Chat Gpt and Bito AI}‬


‭include <iostream>‬
#
using namespace std;‬

‭lass A‬
c
{‬

public:‬

void showA()‬

{‬

cout << "Euler circuit is a circuit containing all the edges of‬

a graph" << endl;‬

}‬

};‬

‭lass C : public A‬
c
{‬

};‬

‭lass D : public A‬
c
{‬

};‬

‭lass E : public C, public D‬


c
{‬

};‬

‭nt main()‬
i
{‬

E eObject;‬

eObject.C::showA();
‭ // Calls the showA function from class C‬
eObject.D::showA();
‭ // Calls the showA function from class D‬
return 0;‬

}‬

Task-4‬

‭include <iostream>‬
#
#include <string>‬

using namespace std;‬


‭lass House‬
c
{‬

protected:‬

string houseName;‬

string sigil;‬

string motto;‬

public:‬

House(string name, string sigil, string motto) : houseName(name),‬

sigil(sigil), motto(motto)‬

{‬

}‬

‭oid displayHouseInfo()‬
v
{‬

cout << "House: " << houseName << endl;‬

‭out << "Sigil: " << sigil << endl;‬
c
cout << "Motto: " << motto << endl;‬

}‬

};‬

‭lass Personality‬
c
{‬

protected:‬

string characterName;‬

string alignment;‬

string traits;‬

public:‬

Personality(string name, string alignment, string traits) :‬

characterName(name), alignment(alignment), traits(traits)‬

{‬

}‬

void displayPersonality() {‬

cout << "Character Name: " << characterName << endl;‬

cout << "Alignment: " << alignment << endl;‬

cout << "Traits: " << traits << endl;‬

}‬

};‬

‭lass Skills‬
c
{‬

protected:‬

int combatSkills;‬

int leadership;‬

string specialAbilities;‬

public:‬

Skills(int combatSkills, int leadership, string abilities) :‬

combatSkills(combatSkills), leadership(leadership),‬

specialAbilities(abilities)‬

{‬

}‬

‭oid displaySkills()‬
v
{‬

cout << "Combat Skills: " << combatSkills << endl;‬

cout << "Leadership: " << leadership << endl;‬

cout << "Special Abilities: " << specialAbilities << endl;‬

}‬

};‬

‭lass GameOfThronesCharacter : public House, public Personality, public‬


c
Skills‬

‭‬
{
public:‬

GameOfThronesCharacter(string houseName, string sigil, string‬

motto, string characterName, string alignment, string traits, int‬

combatSkills, int leadership, string abilities) : House(houseName,‬

sigil, motto), Personality(characterName, alignment, traits),‬

Skills(combatSkills, leadership, abilities)‬

{‬

}‬

‭oid displayCharacterInfo()‬
v
{‬

displayHouseInfo();‬

displayPersonality();‬

displaySkills();‬

}‬

};‬

‭nt main()‬
i
{‬

GameOfThronesCharacter character("Stark", "Direwolf", "Winter is‬

Coming","Jon Snow", "Neutral Good", "Honest, Brave",8, 7, "Warg,‬

Swordsmanship");‬

character.displayCharacterInfo();‬

return 0;‬

}‬

You might also like