Coding Made Easy
In this blog I am providing you major concepts of of Java, C++, C & Algorithms etc.
Monday, 22 April 2024
Why Learning to Code Matters in Computer Science
Monday, 20 November 2023
Panjab University Chandigarh, B.C.A. Semester 1 All Previous Year Questions C Programming
Github Link:
#BCA #PUCHD #coding #bca1 #bcaprograms #cprograms #important #cprogramming
Friday, 24 March 2023
NCERT Class 8th Textbook Mathematics/Ganit/Riyazi
|
|
|
Sunday, 26 February 2023
Pointers in C Programming
In this post we are going to discuss:
- Variables and their Memory Addresses
- Pointers
- Storing Address of Pointer
- Pointer Arithmetic
Variables:
Let us say address of variable a is 2004, so we can represent it as:
Value of a is 10 or Value at Address 2004 is 10
Address of a is 2004
Pointers : These are special variables which are used to store address of another variable. In C language we can declare a pointer using following syntax:
Here data-type is any built-in or user defined data type.
Output :
In first printf we are printing value of a
In second printf will display address of a i.e. 2004,
In 3rd printf we are printing value at address stored in ptr i.e. value at 2004 which is also 10
In last printf it will simply display the address stored in ptr i.e. 2004.
printf("%u",&ptr);
It will display address of ptr which is 3002.
printf("%d",*(*(&ptr)));
Let us evaluate above printf: &ptr is 3002 so it becomes *(*3002), now value at 3002 is 2004 again
value at 2004 is 10. So it will print value of a which is 10.
How to store address of a pointer?
Pointer Arithmetic
Note: Pointer variables are used to store addresses of memory which can be related to addresses of some houses. Like we usually say that my house is located after or before 3 (or any other number) houses of some person. It means we usually add or subtract numbers from real house addresses. We also say that there is a difference of 2 (or any other number) houses in my house and his/her house.
Sunday, 22 January 2023
Defining member functions in C++
Defining member functions in C++
In C++ when we create a function inside a class it is known as member function. A member function in C++ can be defined in two different ways:
- We can define function inside the class i.e. body of the member function will be provided inside class definition.
- We can declare function inside the class i.e. its prototype will be declared inside the class definition but its definition i.e. its body will be defined outside the class.
Example:
Output:
a = 10
b = 20
Click Here to Download Source Code
Another way is to give prototype of function inside the class i.e. function can be declared inside the class and its definition is provided outside the class. If we define a member function outside the class then we have to use scope resolution operator to differentiate it from functions of other classes.
Example:
Output:
a = 10
b = 20
Click Here to Download Source Code
In case we declare member function inside the class then location of its declaration will decide its visibility i.e. it is public, private or protected. In our case both member functions are declared in public section so they are public in nature.
#c++ #cpp #scoperesolution #memberfunctions #cplusplus #functions #definition #coding #codingmadeasy #codingmadeasy #clanguage #cprogramming #java #javaprogramming #datastructure #algorithms #complexity #coding #codeforfun #codinglife #coderlife #programming #program #computerscience #it #technology #ugc #gate #gate #nta #net
Wednesday, 11 January 2023
Scope Resolution (::) Operator in C++
Scope Resolution (::) Operator in C++
- It is used to access global variable in a program if it has same name as of a local variable in a function.
- It is used to define static variables outside class.
- It is used to access static member functions and static data members outside class.
- It is used to define a member function outside the class.
- It is used to resolve ambiguity in case of multiple inheritance.
- It is used to refer data members and member functions of a nested class.
- It is used to access classes and objects from a namespace.
Now we will discuss each of the above uses one by one with suitable examples.- If we declare a global variable in a C++ program and it has same name as of some local variable in a function then by default local variable is accessed & it hides the visibility of global variable.
- Static variables declared within a class must also be defined outside the class, for this scope resolution operator is used.
- Scope resolution operator is also used to access static members i.e. static data members (class variables) and static member functions outside the class. Example:
- In C++ we can define member functions inside as well as outside the class. If member function is to defined outside the class then its declaration must be provided inside the class and definition must begin with identity label using class name and scope resolution operator like:
- In case of multiple inheritance if more than one base class has same name member function, then the the derived class will have definition of both the functions but when one of the function is called then an error will be there. (Ambiguity in multiple inheritance)
- Sometime we define a class within another class which is known as nested class, to declare object of the nested class we use scope resolution operator.
- We can use objects from a namespace without using namespace declaration. It can be achieved with help of scope resolution operator.
Tuesday, 30 August 2022
Fastest Method to Convert Binary Number into Decimal Number