using aggregate initialization on classes and structs

aggregate initialization syntax
1
Type objectName = {argument1,...,argumentN}
alternatively
1
Type objecname {argument1,...,argumentN}
aggregate initialization applied to classes and structs
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
#include <iostream>
#include <string>
using namespace std;

class Aggregate1
{
public:
int num;
double pi;
};

class Aggregate2
{
public:
char hello[6];
int impYear[3];
string world;
};

int main()
{
Aggregate1 a1{2023, 3.14};
cout << "approximately pi is " << a1.pi << endl;

Aggregate2 a2{{'h', 'e', 'l', 'l', 'o', '\0'}, {2020, 2021, 2023}, {"test"}};
cout << "the string is " << a2.world << endl;

return 0;
}

constexpr with classes and objects

The basic effect is improving the performance of application.

e.g. using constexpr with class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

class Human
{
int Age;

public:
constexpr Human(int HumanAge) : Age(HumanAge) {}
constexpr int GetAge() const { return Age; }
};
int main()
{
constexpr Human somePerson(15);
const int hisAge = somePerson.GetAge();
//cout << hisAge << endl;
Human anotherPerson(18);

return 0;
}

uncomment line 16 output is 15. Comparing line 17 and 14-15, the later is optimized for performance.

differences in using dot operator. and pointer operator->

If you have a pointer to an object, the pointer operator would be best suited. If you have instantiated an object as a local variable on the stack, then the dot operator is best suited.

Q:

All my class members are private, and my class does not contain any declared friend class or function. Who can access these members?

A:

None except member methods of the same class ref 1

members of a class are private by default as opposed to those in a struct

e.g.

1
2
3
4
class Human
{
int Age;//Age is private
};

contrarily

1
2
3
4
struct Human
{
int Age;// Age is public
};

A programme for calculating area and circumference of a circle

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
#include <iostream>
#include <string>
using namespace std;

class CalCircle
{
double Radius;
const double Pi;

public:
CalCircle(double urRadius)
: Radius(urRadius), Pi(3.14) {}
void GetArea()
{
double circleArea = 0;
circleArea = Pi * Radius * Radius;
cout << "the area is " << circleArea << endl;
}

void GetCircumference()
{
double circleCircumference=0;
circleCircumference=2*Pi*Radius;

cout <<" the circumference is " << circleCircumference<< endl;
}
};

int main()
{
double yourRadius=0;
cout << "plz input ur Radius ";
cin >>yourRadius;

CalCircle MyCircle(yourRadius);
MyCircle.GetArea();

return 0;
}
Note that

: line 11-12 cuz Pi is a const variable, it can be initialized not included in constructor CalCircle as a parameter. e.g.

1
2
CalCircle(double urRadius, const double urPi)
: Radius(urRadius), Pi(urPi) {}

Line13-18 members methods of same class can access private member Ref 1