Skip to content

Commit 2fce57c

Browse files
committed
add 42 to 73 for c++
1 parent 4bbb70c commit 2fce57c

13 files changed

+437
-0
lines changed

C++/50_51_cppBeginners.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//This is Sally.h file
2+
#ifndef SALLY_H
3+
#define SALLY_H
4+
5+
class Sally
6+
{
7+
public:
8+
int num;
9+
Sally();
10+
Sally(int);
11+
Sally operator+(Sally);
12+
}
13+
//This is Sally.cpp file
14+
#include <iostream>
15+
#include "Sally.h"
16+
using namespace std;
17+
18+
Sally::Sally()
19+
{
20+
}
21+
22+
Sally::Sally(int a){
23+
num = a;
24+
}
25+
Sally Sally::operator+(Sally aso){
26+
Sally brandNew;
27+
brandNew.num = num + aso.num;
28+
return(brandNew);
29+
}
30+
//This is main.cpp file
31+
#include <iostream>
32+
#include "Sally.h"
33+
using namespace std;
34+
35+
int main()
36+
{
37+
Sally a(34);
38+
Sally b(21);
39+
Sally c;
40+
c=a+b;
41+
cout << c.num << endl;
42+
43+
}

C++/52_cppBeginners.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//This is Mother.h file
2+
#ifndef MOTHER_H
3+
#define MOTHER_H
4+
5+
class Mother
6+
{
7+
public:
8+
Mother();
9+
void sayName();
10+
};
11+
12+
#endif // MOTHER_H
13+
14+
//This is Mother.cpp file
15+
#include <iostream.h>
16+
#include "Mother.h"
17+
#include "Daughter.h"
18+
using namespace std;
19+
20+
Mother::Mother()
21+
{
22+
}
23+
24+
void Mother::sayName(){
25+
cout << "I am a RobnertsQ" << endl;
26+
}
27+
28+
//This is Daughter.h file
29+
#ifndef DAUGHTER_H
30+
#define DAUGHTER_H
31+
32+
class Daughter: public Mother
33+
{
34+
public:
35+
Daughter();
36+
};
37+
38+
#endif // DAUGHTER_H
39+
40+
//This is Daughter.cpp file
41+
#include <iostream.h>
42+
#include "Mother.h"
43+
#include "Daughter.h"
44+
using namespace std;
45+
46+
Daughter::Daughter()
47+
{
48+
}
49+
//This is main.cpp file
50+
#include <iostream.h>
51+
#include "Mother.h"
52+
#include "Daughter.h"
53+
using namespace std;
54+
55+
int main(){
56+
57+
Daughter tina;
58+
tina.sayName();
59+
}

C++/53_cppBeginners.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//This is Mother.h file
2+
#ifndef MOTHER_H
3+
#define MOTHER_H
4+
5+
class Mother
6+
{
7+
public:
8+
int publicv;
9+
protected:
10+
int protectedv;
11+
private:
12+
int privatev;
13+
};
14+
15+
#endif // MOTHER_H
16+
17+
//This is Mother.cpp file
18+
#include <iostream.h>
19+
#include "Mother.h"
20+
#include "Daughter.h"
21+
using namespace std;
22+
23+
//This is Daughter.h file
24+
#ifndef DAUGHTER_H
25+
#define DAUGHTER_H
26+
27+
class Daughter: public Mother
28+
{
29+
public:
30+
void dosomething();
31+
};
32+
33+
#endif // DAUGHTER_H
34+
35+
//This is Daughter.cpp file
36+
#include <iostream.h>
37+
#include "Mother.h"
38+
#include "Daughter.h"
39+
using namespace std;
40+
41+
void Daughter::dosomething()
42+
{
43+
publicv = 1;
44+
protected = 2;
45+
}
46+
//This is main.cpp file
47+
#include <iostream.h>
48+
#include "Mother.h"
49+
#include "Daughter.h"
50+
using namespace std;
51+
52+
int main(){
53+
54+
Daughter tina;
55+
tina.dosomething();
56+
}
57+

C++/54_cppBeginners.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//This is Mother.h file
2+
#ifndef MOTHER_H
3+
#define MOTHER_H
4+
5+
class Mother
6+
{
7+
public:
8+
Mother();
9+
~Mother();
10+
};
11+
12+
#endif // MOTHER_H
13+
14+
//This is Mother.cpp file
15+
#include <iostream.h>
16+
#include "Mother.h"
17+
#include "Daughter.h"
18+
using namespace std;
19+
20+
Mother::Mother(){
21+
cout<< "I am the mother constructor!" << endl;
22+
}
23+
24+
Mother::~Mother(){
25+
cout<< "mother deconstructor!" << endl;
26+
}
27+
//This is Daughter.h file
28+
#ifndef DAUGHTER_H
29+
#define DAUGHTER_H
30+
31+
class Daughter: public Mother
32+
{
33+
public:
34+
Daughter();
35+
~Daughter();
36+
};
37+
38+
#endif // DAUGHTER_H
39+
40+
//This is Daughter.cpp file
41+
#include <iostream.h>
42+
#include "Mother.h"
43+
#include "Daughter.h"
44+
using namespace std;
45+
46+
void Daughter::dosomething()
47+
{
48+
publicv = 1;
49+
protected = 2;
50+
}
51+
//This is main.cpp file
52+
#include <iostream.h>
53+
#include "Mother.h"
54+
#include "Daughter.h"
55+
using namespace std;
56+
57+
int main(){
58+
59+
Daughter tina;
60+
61+
}
62+

C++/55_cppBeginners.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Enemy{
5+
protected:
6+
int attackPower;
7+
public:
8+
void setAttackPower(int a){
9+
attackPower=a;
10+
}
11+
};
12+
class Ninja: public Enemy{
13+
public:
14+
void attack()
15+
{cout << "i am a ninja, ninja chop! -" << attackPower <<endl;}
16+
};
17+
18+
class Monster: public Enemy{
19+
public:
20+
void attack()
21+
{cout << "monster must eat you!!!! -" << attackPower << endl;}
22+
};
23+
int main()
24+
{
25+
Ninja n;
26+
Monster m;
27+
Enemy *enemy1 = &n;
28+
Enemy *enemy2 = &m;
29+
enemy1->setAttackPower(29);
30+
enemy2->setAttackPower(99);
31+
n.attack();
32+
m.attack();
33+
}

C++/56_cppBeginners.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Enemy{
5+
public:
6+
virtual void attack(){}
7+
8+
};
9+
class Ninja: public Enemy{
10+
public:
11+
void attack()
12+
{cout << "ninja attack!" <<endl;}
13+
};
14+
15+
class Monster: public Enemy{
16+
public:
17+
void attack()
18+
{cout << "monster attack!" << endl;}
19+
};
20+
int main()
21+
{
22+
Ninja n;
23+
Monster m;
24+
Enemy *enemy1 = &n;
25+
Enemy *enemy2 = &m;
26+
enemy1->attack();
27+
enemy2->attack();
28+
29+
}

C++/57_cppBeginners.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Enemy{
5+
public:
6+
virtual void attack()=0;
7+
8+
};
9+
class Ninja: public Enemy{
10+
public:
11+
void attack()
12+
{cout << "ninja attack!" <<endl;}
13+
};
14+
15+
class Monster: public Enemy{
16+
public:
17+
void attack()
18+
{cout << "monster attack!" << endl;}
19+
};
20+
int main()
21+
{
22+
Ninja n;
23+
Monster m;
24+
Enemy *enemy1 = &n;
25+
Enemy *enemy2 = &m;
26+
enemy1->attack();
27+
enemy2->attack();
28+
29+
}

C++/58_cppBeginners.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
template <class bucky>
5+
bucky addCrap(bucky a, bucky b)
6+
{
7+
return a+b;
8+
}
9+
10+
int main()
11+
{
12+
int x=7, y=43,z;
13+
z=addCrap(x,y);
14+
cout << z <<endl;
15+
}

C++/59_cppBeginners.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
template <class FIRST, class SECOND>
5+
6+
FIRST smaller(FIRST a, SECOND b)
7+
{
8+
return (a<b?a:b);
9+
}
10+
11+
int main()
12+
{
13+
int x=89;
14+
double y=56.78;
15+
cout << smaller(x,y) <<endl;
16+
}

C++/60_cppBeginners.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
template <class T>
5+
class Bucky{
6+
T first, second;
7+
public:
8+
Bucky(T a, T b)
9+
{
10+
first=a;
11+
second=b;
12+
}
13+
T bigger();
14+
};
15+
template <class T>
16+
T Bucky<T>::bigger()
17+
{
18+
return (first>second?first:second);
19+
}
20+
21+
int main()
22+
{
23+
Bucky <int> bo(258, 105);
24+
cout << bo.bigger();
25+
}

0 commit comments

Comments
 (0)