The bsisc codes for C++:
1. I use the bot operation to invoke the property value and its function ; get the following outputs:
output the default value of raidus5.2
output the setting value of raidus7.8
* the first output is use the constructors function without any input arguments
* the second output use the constructors function with an double argument, please notice the recall methods in the following corresponding codes:
//objective: know the basic creation and uses for class and its corresponding objective iterm
#include <iomanip>
#include <iostream>
int main()
{
class Circles{
public: double radius;
//constructors function(no any return values; with same name of class)
Circles(){
radius = 5.2;
}
Circles(double len){
radius = len;
}
double areas(){
return radius * radius * 3.14159265;
}
};
Circles c_1;
std::cout << "output the default value of raidus" << c_1.radius << std::endl;
Circles c_2{7.8};
std::cout << "output the setting value of raidus" << c_2.radius << std::endl;
return 0;
}
I know you must have notice the above codes has a function called areas. It is obvious that this function could give you the area of the circle. The following codes will show you how to invoke this function by its objectives:
outputs:
output the default value of raidus5.2
output the areas of circles with raidus c_1: 84.9487
output the setting value of raidus7.8
output the areas of circles with radius c_2: 191.134
The corresponding codes:
//objective: know the basic creation and uses for class and its corresponding objective iterm
#include <iomanip>
#include <iostream>
int main()
{
class Circles{
public: double radius;
//constructors function(no any return values; with same name of class)
Circles(){
radius = 5.2;
}
Circles(double len){
radius = len;
}
double areas(){
return radius * radius * 3.14159265;
}
};
Circles c_1;
std::cout << "output the default value of raidus" << c_1.radius << std::endl;
std::cout << "output the areas of circles with raidus c_1: " << c_1.areas() << std::endl;
Circles c_2{7.8};
std::cout << "output the setting value of raidus" << c_2.radius << std::endl;
std::cout << "output the areas of circles with radius c_2: " << c_2.areas() << std::endl;
return 0;
}
The following codes may be more suitable to the standard outputs:
The default radius of the circle_1 is: 1
The radius of the now circle_1 is: 4.6
The area of the now circle_1 is: 66.476
The default radius of the circle_2 is: 5.6
The area of the now circle_2 is: 98.5203
The corresponding codes:
//objective: know the basic creation and uses for class and its corresponding objective iterm
//use the code framework in the videos I have seen before
#include <iostream>
#include <cmath>
//define a Circle class in the global area
class Circle{
public:
double radius;
Circle(){
radius = 1.0;
}
Circle(double newRadius){
radius = newRadius;
}
double getArea(){
return pow(radius, 2.0) * 3.14159;
}
};
int main()
{
Circle circle_1;
std::cout << "The default radius of the circle_1 is: " << circle_1.radius << std::endl;
// change the radius of the circle_1 to 4.6
circle_1.radius = 4.6;
std::cout << "The radius of the now circle_1 is: " << circle_1.radius << std::endl;
std::cout << "The area of the now circle_1 is: " << circle_1.getArea() << std::endl;
Circle circle_2{5.6};
std::cout << "The default radius of the circle_2 is: " << circle_2.radius << std::endl;
std::cout << "The area of the now circle_2 is: " << circle_2.getArea() << std::endl;
return 0;
}