c++ code
c++ code
#include <iostream>
using namespace std;
class Birthday{
public:
Birthday(int d, int m, int y){
day = d;
month = m;
year = y;
};
void printDate() {
cout << day << "/" << month << "/" << year << endl;
}
private:
int day;
int month;
int year;
};
class People {
public:
People(string x, Birthday bo)
: name(x), dateOfBirth(bo)
{
}
void printInfo(){
cout << name << " was born on " ;
dateOfBirth.printDate();
}
private:
string name;
Birthday dateOfBirth;
};
int main() {
Birthday bop(16,11,2000);
People po("Guruvansh", bop);
po.printInfo();
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for(int x; x <=1;x++){
}
if(n/2==0){
return n*2;
}
if(n/2!=0){
return n*3 + 1;
}
else{
return 1;
}
return 0;
}
int main() {
void printByValue(int x) {
x = 24343;
}
void printByReference(int *x) {
*x = 131312;
}
class Sally{
public:
void printCrap(int x, int y){
a = x;
b = y;
sum = a+b;
cout << sum;
}
private:
int a;
int b;
int sum;
};
int main() {
Sally sm;
sm.printCrap(23,54);
//friend function
// friend function
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class StankFist{
public:
StankFist() {
stinkyVar = 0;
}
friend void stinkyFriend(StankFist &sfo);
private:
int stinkyVar;
};
void stinkyFriend(StankFist &sfo){
sfo.stinkyVar = 69;
cout << "changing value of class function through outside function by accessing
it through the friend function "<< sfo.stinkyVar << endl;
}
int main() {
StankFist jj;
stinkyFriend(jj);
}
////
Enemy ninja and monster
// program based on polymorphism :
#include <iostream>
using namespace std;
class Enemy{
public:
void setAttackPower(int x)
{
attackPower = x;
}
void attack() {
cout << "VOid attack by RegEnemy" << endl;
}
protected:
int attackPower;
};
class Ninja: public Enemy {
public:
void attack() {
cout << "NInja attack ! Ninja chop! Chop! chop! -" << attackPower
<< endl;
}
};
class Monster: public Enemy {
public:
void attack() {
cout << "Monster! Roarrr.... Monster will ear you! " << attackPower
<< endl;
}
};
int main() {
Ninja n;
Monster m;
Enemy *en = &n;
Enemy *em = &m;
en->setAttackPower(360);
em->setAttackPower(888);
n.attack();
m.attack();
RegEnemy re;
Enemy *eo = &re;
re.attack();
return 0;
}