Professional Documents
Culture Documents
Lab 12 22235103361
Lab 12 22235103361
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__
(
self
,
name
,
max_health
):
self
.
name
=
name
self
.
max_health
=
max_health
self
.
health
=
max_health
def
take_damage
(
self
,
damage
):
self
.
health
-=
damage
if
self
.
health
<
0:
self
.
health
=
0
def
heal
(
self
,
amount
):
self
.
health
+=
amount
if
self
.
health
>
self
.
max_health
:
self
.
health
=
self
.
max_health
def
get_health
(
self
):
return
self
.
health
def
is_alive
(
self
):
return
self
.
health
>
0
class
Player1
(
Player_Basic
):
def
__init__
(
self
,
name
):
super
().
__init__
(
name
,
max_health
=
100
)
class
Player2
(
Player_Basic
):
def
__init__
(
self
,
name
):
super
().
__init__
(
name
,
max_health
=
120
)
class
Player3
(
Player_Basic
):
def
__init__
(
self
,
name
):
super
().
__init__
(
name
,
max_health
=
100
)
# Example usage:
player1
=
Player1
(
"Player 1"
)
player2
=
Player2
(
"Player 2"
)
player3
=
Player3
(
"Player 3"
)
print
(
f
"
{
player1
.
n
ame
}
's health:
{
player1
.
get_health
()
}
"
)
print
(
f
"
{
player2
.
n
ame
}
's health:
{
player2
.
get_health
()
}
"
)
print
(
f
"
{
player3
.
n
ame
}
's health:
{
player3
.
get_health
()
}
"
)
player1
.
take_damage
(
20
)
player1
.
heal
(
20
)
player2
.
take_damage
(
20
)
player2
.
heal
(
30
)
player2
.
heal
(
0
)
player2
.
take_damage
(
100
)
print
(
f
"
{
player1
.
n
ame
}
's health:
{
player1
.
get_health
()
}
"
)
print
(
f
"
{
player2
.
n
ame
}
's health:
{
player2
.
get_health
()
}
"
)
if
player3
.
is_alive
():
print
(
f
"
{
player3
.
name
}
is alive."
)
else
:
print
(
f
"
{
player3
.
name
}
is defeated."
)
lass Player
c
{
public:
string name;
int age;
string country;
}
};
}
};
}
};
}
};
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;
}
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
{
};
};
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>
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;
}
};
}
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;
}