-
Notifications
You must be signed in to change notification settings - Fork 0
/
Classes.cpp
131 lines (119 loc) · 2.74 KB
/
Classes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<bits/stdc++.h>
using namespace std;
namespace example1 {
class triangle
{
private:
float base;
float height;
public:
// SET function
void setBase_height(float b, float h)
{
base = b;
height = h;
}
// GET area
float getArea()
{
return 0.5*base*height;
}
// print function
void print()
{
cout << "Base = " << base << endl <<
"Height = " << height << endl <<
"Area = " << getArea() << endl;
}
};
void run()
{
triangle t1;
t1.setBase_height(10, 20);
t1.getArea();
cout << t1.getArea() << endl; // 100
t1.print(); // 10 20 100
}
}
namespace example2 {
class car
{
// Private scope
private:
// Member data/values
char name[15];
char color[10];
int maxSpeed;
int model;
// Public scope
public:
// Member functions --> Functions defined in the class in the public scope
// SET FUNCTIONS
// Function to set a name for the cat
void setName(char n[])
{
strcpy_s(name, n);
}
// function to set a color
void setColor(char c[])
{
strcpy_s(color, c);
}
// function to set a maxSpeed
void setMaxspeed(int m)
{
maxSpeed = m;
}
//function to set the model
void setModel(int m)
{
model = m;
}
// GET FUNCTIONS
char* getName()
{
return name;
}
char* getColor()
{
return color;
}
int getMaxspeed()
{
return maxSpeed;
}
int getModel()
{
return model;
}
// Function to print the attributes values
void print()
{
/*
cout << "Car name: " << name << "\n" <<
"Car color: " << color << "\n" <<
"Car max speed = " << maxSpeed << "\n"
"Car model: " << model << "\n";
*/ //OR
cout << "Car name: " << getName() << "\n" <<
"Car color: " << getColor() << "\n" <<
"Car max speed = " << getMaxspeed() << "\n"
"Car model: " << getModel() << "\n";
}
};
// Non-member function
void p()
{
cout << "Non member function" << endl;
}
void run()
{
p();
car x;
x.setName("BMW");
x.setColor("Blue");
x.setMaxspeed(250);
x.setModel(2023);
x.print();
}
}