Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
muhibarshad committed Oct 16, 2022
2 parents a54e8f0 + 1b33ebd commit 1f21290
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 1 deletion.
185 changes: 184 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
- [Overloaded Constructor](#overloaded-constructor)
- [Copy Constructor](#copy-constructor)
- [Destructor](#destructors)

14. [Const in oop](#const-in-oop)
- [const data member](#const-data-members)
- [const member Function](#const-member-functions)
- [const obj](#const-object)

# Dynamic Arrays

> Dynamic memory allocation is the process of changing the size of the memory space during the run-time.
Expand Down Expand Up @@ -1676,3 +1680,182 @@ Accessing of these objects is same as the simple arrays `dot(.) operator` for th
<p align="center">
<img src="/Some%20extra%20concepts/codeSnaps/dot.png" style="height: 70vh; padding-left: 50vh;">
</p>
# Const in oop
## Const Data Members
### Defination
>The `data members`,state or properties whose value can not be changed during execution time and require value at time upon intialization .
### Note
> You can not left const memmber uninitialized,otherwise it will cause error!
### Ways to declare and initialize
1) Initializing at time of declarartion
<p align="center">
<img src="/Some%20extra%20concepts/codeSnaps/constdatamember.png" style="height: 70vh; padding-left: 50vh;">
</p>
2)Initializing in `Initializer list` of `constructors`
<p align="center">
<img src="/Some%20extra%20concepts/codeSnaps/connst_In_initializerlist.png" style="height: 70vh; padding-left: 50vh;">
</p>
### Example Code
```cpp
#include<iostream>
using namespace std;
class Student
{
String name;
// 1) initializing const member at time of creation
const int Id=1;
const double CNIC;
public:
// 2)initializing const member in member initialization list
Student():CNIC(123-456789-123) {}
};
```

## Const Member functions

### Defination
>Tese are `member functions` of class which allow only *memory reading*.It simply means we can not change any `data member` of class either const or non-const `data member` in Const member functions.The can be accessed by both `const object` and `non-const object`.
### Note
> `constructors` are also *special member functions* of class but it is not allowed to make constructors const functions.

<p align="center">
<img src="/Some%20extra%20concepts/codeSnaps/const_functionn.png" style="height: 70vh; padding-left: 50vh;">
</p>

### Example code
```cpp

#include<iostream>
using namespace std;

class Student
{
String name;
const int Id = 1;

public:

//-----Getters/Accessors----

string getName() const
{
//Here if you try to modify data member name,it will cause error although it is non-const
// like:
// name="Waleed";
return name;

}

int getId() const
{
//Here if you try to modify data member Id,it will cause error
// like:
// Id=9;
return Id;

}



};
int main()
{
Student student_1;
Student const student_2;

//Accessing constant function by Non const-object
student_1.getId();

//Accessing constant function by const-object
student_2.getId();

}

```

## Const Object

### Defination
> Tese are `objects` of class which allow only *memory reading*.It simply means we can not change any `data member` of class either const or non-const `data member`.
### Note
> const objects give their `refrence` to only const member functions/behaviours.
<p align="center">
<img src="/Some%20extra%20concepts/codeSnaps/const_obj.png" style="height: 70vh; padding-left: 50vh;">
</p>

```cpp

#include<iostream>
using namespace std;

class Student
{
String name;
const int Id = 1;

public:

//-----Getters/Accessors----

string getName()
{
return name;

}

int getId() const
{
return Id;

}



};
int main()
{

Student const Student_1;
//getId is constant member function,Therfore const
//obj studen_1 give its refrence
Student_1.getId();


//getName is not_ constant member function,Therfore const
//obj studen_1 does not give its refrence
// Error---> Student_1.getName();


}
```




Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Some extra concepts/codeSnaps/const_function.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Some extra concepts/codeSnaps/const_obj.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Some extra concepts/codeSnaps/constdatamember.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1f21290

Please sign in to comment.