Skip to content

Commit 42b2ec4

Browse files
Create constants&objects
1 parent 7b9fc99 commit 42b2ec4

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

constants&objects

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/* Constants
2+
3+
A constant is an expression with a fixed value. It cannot be changed while the program is run.Use the const keyword to define a constant variable.
4+
All constant variables must be initialized at the time of their creation.
5+
*/
6+
const int x = 42;
7+
8+
#include <iostream>
9+
using namespace std;
10+
11+
int main() {
12+
return 0;
13+
}
14+
/*
15+
to be more precise, you cannot change x value through its name, in other words, if I create a pointer to x, let's say p, then *p=2 for example will perfectly work : try this for example:
16+
it will print 3,which means that the value of x actually changed
17+
*/
18+
// BAD CODE BAD CODE
19+
int main(){
20+
const int x=1;
21+
int *ptr=&x;
22+
*ptr=3;
23+
cout<<x;
24+
return 0;
25+
}
26+
27+
// same goes for references, for instance :
28+
int a=2;
29+
const int& b(a);
30+
a=3;
31+
cout<<b;
32+
33+
// it will print 3
34+
// Type in the missing keyword to declare a constant variable named ''var'' of type double
35+
const double var = 3.4;
36+
37+
/*
38+
As with the built-in data types, we can make class objects constant by using the const keyword.
39+
const MyClass obj;
40+
41+
All const variables must be initialized when they're created. In the case of classes, this initialization is done via constructors. If a class is not initialized using a parameterized constructor, a public default constructor must be provided - if no public default constructor is provided, a compiler error will occur.
42+
43+
Once a const class object has been initialized via the constructor, you cannot modify the object's member variables. This includes both directly making changes to public member variables and calling member functions that set the value of member variables.
44+
When you've used const to declare an object, you can't change its data members during the object's lifetime.
45+
46+
Saunders it's also a hint for the compiler to optimize. Every operation you make with constants is evaluated at compile time. For example, if you have a const c = 100 and somewhere in the code you have sqrt(c), it actually gets replaced with the result instead of the function call, thus saving time when the program runs, and some memory.
47+
*/
48+
49+
// Drag and drop from the options below to declare an object named ''st'' of type Student, then call its printAge() function
50+
51+
Student,st;
52+
st. printAge();
53+
54+
/*
55+
Only non-const objects can call non-const functions.
56+
A constant object can't call regular functions. Hence, for a constant object to work you need a constant function.
57+
58+
To specify a function as a const member, the const keyword must follow the function prototype, outside of its parameters' closing parenthesis. For const member functions that are defined outside of the class definition, the const keyword must be used on both the function prototype and definition. For example:
59+
*/
60+
MyClass.h
61+
62+
class MyClass
63+
{
64+
public:
65+
void myPrint() const;
66+
};
67+
68+
MyClass.cpp
69+
#include "MyClass.h"
70+
#include <iostream>
71+
using namespace std;
72+
73+
void MyClass::myPrint() const {
74+
cout <<"Hello"<<endl;
75+
}
76+
// Now the myPrint() function is a constant member function. As such, it can be called by our constant object:
77+
int main() {
78+
const MyClass obj;
79+
obj.myPrint();
80+
}
81+
// Outputs "Hello"
82+
a)Non constant objects can call functions which are const or non-const.
83+
// declare a constant printAge() member function for the Student class
84+
85+
class Student
86+
{
87+
public:
88+
void printAge() const;
89+
};
90+
91+
// add the 'mutable' keyword at the begining of the variable declaration
92+
93+
#include <iostream>
94+
using namespace std;
95+
96+
class MyClass
97+
{
98+
public:
99+
MyClass();
100+
void myPrint() const;
101+
mutable int cnt;
102+
};
103+
104+
void MyClass::myPrint() const {
105+
cout <<"Hello "<<cnt<<endl;
106+
cnt++;
107+
}
108+
109+
MyClass::MyClass()
110+
{
111+
MyClass::cnt = 0;
112+
}
113+
114+
int main() {
115+
const MyClass obj;
116+
obj.myPrint();
117+
obj.myPrint();
118+
obj.myPrint();
119+
}

0 commit comments

Comments
 (0)