top of page

Hiding Names

When you declare a program element such as a class, function, or variable, its name can only be "seen" and used in certain parts of your program. The context in which a name is visible is called its scope. For example, if you declare a variable x within a function, x is only visible within that function body. It has local scope. You may have other variables by the same name in your program; as long as they are in different scopes, they do not violate the One Definition Rule and no error is raised.

For automatic non-static variables, scope also determines when they are created and destroyed in program memory.

There are six kinds of scope:

  • Global scope A global name is one that is declared outside of any class, function, or namespace. However, in C++ even these names exist with an implicit global namespace. The scope of global names extends from the point of declaration to the end of the file in which they are declared. For global names, visibility is also governed by the rules of linkage which determine whether the name is visible in other files in the program.

  • Namespace scope A name that is declared within a namespace, outside of any class or enum definition or function block, is visible from its point of declaration to the end of the namespace. A namespace may be defined in multiple blocks across different files.

  • Local scope A name declared within a function or lambda, including the parameter names, have local scope. They are often referred to as "locals". They are only visible from their point of declaration to the end of the function or lambda body. Local scope is a kind of block scope, which is discussed later in this article.

  • Class scope Names of class members have class scope, which extends throughout the class definition regardless of the point of declaration. Class member accessibility is further controlled by the public, private, and protected keywords. Public or protected members can be accessed only by using the member-selection operators (. or ->) or pointer-to-member operators (.* or ->*).

  • Statement scope Names declared in a for, if, while, or switch statement are visible until the end of the statement block.

  • Function scope A label has function scope, which means it is visible throughout a function body even before its point of declaration. Function scope makes it possible to write statements like goto cleanup before the cleanup label is declared.

i = 0 i = 7 j = 9 i = 0

Look at the above program. The variable “global” declared at the top is global and stores the value 5 where as that declared within main function is local and stores a value 2. So, the question is when the value stored in the variable named “global” is printed from the main function then what will be the output? 2 or 5?

  • Usually when two variable with same name are defined then the compiler produces a compile time error. But if the variables are defined in different scopes then the compiler allows it.

  • Whenever there is a local variable defined with same name as that of a global variable then the compiler will give precedence to the local variable

How to access a global variable when there is a local variable with same name?

What if we want to do the opposite of above task. What if we want to access global variable when there is a local variable with same name? 
To solve this problem we will need to use the scope resolution operator. Below program explains how to do this with the help of scope resolution operator. 

Advantages of C++:
 

  • C++ is an object-oriented programming language. It may be a collection of commands, which tell the pc to try to do “something.” This collection of commands is typically called C++ ASCII text files.
     

  • C++ could also be a problem-oriented language that’s much easier to use than the other low-level languages like binary coding. It takes much more space than low-level languages but it’s much easier to understand and learn.
     

  • C++ program has many advantages when it involves programming, All C++ program stand-alone files must use the foremost function to allow the program to start out up and motivate its functions.
     

  • C++ program can support unions and structures that are a mix of stand-alone and put-together files, It uses the standard C++ application mentioned as “.cpp”, C++ uses the reserved library word mentioned as “goto” that’s the same as Java’s continue, or break commands.
     

  • The global data and global functions are used within C++ that aren’t utilized in many other high-level languages within the pc sciences and it is an advantage to the programming languages.
     

  • As C++ doesn’t use the objects, it’s difficult to make the programs that have inheritance data and thus the reformed the programs to contribute to the other data and therefore the programs, it is possible to undertake to the present, however, but is difficult, These inheritance data and programs are mentioned because of the inheritance trees.
     

  • C++ doesn’t support class methods that are faithful away, but class methods are basically functions, and sometimes they’re referred to as functions.
     

  • C++ program uses multi-paradigm programming, Paradigm means the planning of programming, paradigm concerned about the logic, the structure, and procedure of program, C++ program is multi-paradigm means it follows three paradigms Generic, Imperative, Object-Oriented.
     

  • C++ program is useful for low-level programming language and really efficient for general purposes, It offers performance and memory efficiently, It offers high-level abstraction, within the language of the matter domain
     

  • C++ may be a system programming and features a relatively clear and mature standard,
     

  • C++ program supports inline function, C++ supports exception handling, it’s pointer and references, C++ uses cin and cout
     

  • C++ has a large and mature ecosystem of libraries and frameworks that can be used to accelerate development and simplify tasks like network programming, graphics rendering, and database access.
     

Disadvantages of C++:
 

  • One major problem in C++ is when the info points to an equivalent thing from two different starting points, this causes a serious problem, the C++ program will still have involved problems within the coding.
     

  • Java’s uni-code to the ASCII rules is 16-bit, while C++ program is 8-bit only, So, C++ may be a less impressive programming language but saves the memory.
     

  • C++ program is complex during a very large high-level program, C++ is employed for platform-specific application commonly, For the actual OS or platform, the library set is typically chosen.
     

  • C++ program can’t support garbage pickup, It’s not secure because it’s a pointer, friend function, and global variable and it’s no support for threads built-in.
     

  • C++ programs are often heavy if it’s not careful, C++ program allows classes and thus the functions with an equivalent name and overloaded functions thus the symbol mangling system must be used, It can easily be wrapped in C functions though.
     

  • C++ program has no notion of being fast and it’s not used for platform-dependent apps any longer than C or anything is. Actually, given the character of the toolchain, it’s probably less dependent than others. Complex in a very large high-level program.
     

  • It is used for platform-specific applications commonly.
     

  • For a specific OS or platform, the library set is typically chosen that locks.
     

  •  C++ is a low-level language and hence it can be more difficult to maintain and debug compared to higher-level languages that provide more abstraction and automation.
     

  • C++ is a complex and powerful language that can take time to learn and master, especially for beginners who are new to programming.
     

Here's a complete roadmap for you to become a developer: Learn DSA -> Master Frontend/Backend/Full Stack -> Build Projects -> Keep Applying to Jobs

And why go anywhere else when our DSA to Development: Coding Guide helps you do this in a single program! Apply now to our DSA to Development Program and our counsellors will connect with you for further guidance & support.

C++ is a versatile and powerful programming language that is widely used in various domains such as system programming, game development, embedded systems, and more. It was developed by Bjarne Stroustrup in the 1980s as an extension of the C programming language, with a focus on adding support for object-oriented programming (OOP) features while maintaining compatibility with C.

One of the key features of C++ is its support for both procedural and object-oriented programming paradigms. This means that developers can write code in a structured, modular manner using functions and procedures, as well as take advantage of features such as classes, objects, inheritance, polymorphism, and encapsulation to build complex and reusable software components.

Classes are fundamental building blocks in C++, allowing developers to encapsulate data and behavior into objects. For example, in a game development scenario, a "Player" class may encapsulate attributes like player health, position, and methods like move() and attack(). Objects are instances of classes, each with their own state (data) and behavior (methods).

Inheritance allows classes to inherit properties and behavior from other classes, enabling code reuse and promoting a hierarchical organization of classes. For instance, a "Monster" class might inherit from a more general "Creature" class, inheriting common attributes and methods while adding its own unique functionality.

Polymorphism allows objects of different classes to be treated as objects of a common superclass, simplifying code and promoting flexibility. For example, a "Shape" superclass may have subclasses like "Circle" and "Rectangle". Code that operates on shapes can treat circles and rectangles uniformly, without needing to know their specific types.

Encapsulation refers to the bundling of data and methods that operate on that data within a single unit, typically a class. This helps to hide the internal implementation details of a class from the outside world, promoting modularity, maintainability, and code reliability.

Another powerful feature of C++ is templates, which allow for generic programming. Templates enable the creation of functions and classes that can operate on generic types, providing a high degree of flexibility and code reuse. For example, a generic sorting function can be implemented using templates to sort arrays of any data type.

Memory management in C++ is a critical aspect of programming, as developers have direct control over memory allocation and deallocation. C++ supports both automatic and manual memory management. Automatic memory management is achieved through stack allocation for local variables and automatic garbage collection for objects allocated on the heap. Manual memory management is done using pointers and dynamic memory allocation with operators like new and delete.

C++ also supports exception handling, allowing developers to handle runtime errors and abnormal conditions gracefully. Exceptions provide a mechanism for transferring control from the point of error detection to a block of code designed to handle the error, improving program robustness and reliability.

The Standard Template Library (STL) is a core part of C++ that provides a rich collection of generic data structures and algorithms. It includes containers like vectors, lists, and maps, as well as algorithms for searching, sorting, and manipulating these containers. The STL promotes code reuse and standardization, making C++ development more efficient and productive.

C++ is also known for its performance and efficiency, making it a popular choice for applications where speed and resource usage are critical. Its close relationship with hardware and low-level system capabilities allows developers to optimize code for specific platforms and leverage hardware features effectively.

In conclusion, C++ is a versatile and powerful programming language that offers a wide range of features and capabilities for building complex and efficient software systems. Its support for procedural and object-oriented programming, along with features like templates, memory management, exception handling, and the STL, make it a popular choice for a variety of application domains. Despite its complexity, mastering C++ can provide developers with the tools they need to create high-performance, maintainable, and scalable software solutions.

  • Array elements can be inserted and printed in C++ and C programming languages. In C++, use the 'push_back' command to add elements to the array. In C programming language, use the 'insert' command to insert array elements and 'printf' to print out the array.

  • Declaring an array in C or C++ is a simple process. In the most basic sense, an array is a collection of related data items that a single variable name can reference. In C and C++, the array is declared using the square bracket ([ ]) notation after the data type.

  • Arrays can be initialized in C and C++ using one of two methods. The first is by assigning each element a separate value. This can be done using a for loop or manually. The second method uses the array initialization list syntax, which uses curly brackets ({}).

  • In C++ and C programming, an array can be accessed using the index of the element that is to be accessed. The syntax for accessing an array element in C++ and C follows the format: [index]. For instance, if we have an array named arr and want to access the third element of that array, we would use the syntax arr3. Similarly, if we have an array named myarr and want to access the fifth element of that array, we would use the syntax myarr5. It's important to note that array indexes start from 0, so if we want to access the first element of an array, we would use the syntax [0].

    Company

  • In C and C++, a multi-dimensional array is an array of arrays. It is composed of two or more dimensions, with each dimension being indexed. Multi-dimensional arrays are usually used to store information in a structured way, allowing for easy access and manipulation of data elements.

Optimizing Tasks: Priority Scheduling Program in C

In the realm of computing, managing tasks effectively is paramount. This is where Priority Scheduling steps in as a powerful technique. 

 

At its core, Priority Scheduling program in C involves arranging tasks based on their relative priorities, ensuring that higher-priority tasks get executed before lower-priority ones. 

 

Imagine juggling multiple tasks in your daily life; you’d naturally tackle the more urgent matters first. Similarly, a Priority Scheduling program in C allows a computer to prioritize tasks intelligently.

Binary Operators in C Programming

Application of a Binary Operator

The practical utilization of binary operators spans across numerous aspects of programming and beyond. Imagine a website’s login system, where the equality operator (==) matches the user’s input password with the stored one. In graphics processing, the bitwise shift operators (<< and >>) aid in swiftly moving pixels to generate animations or apply filters. In data encryption, the XOR operator (^) is pivotal in securing information by transforming it into a scrambled form. 

 

Increment and Decrement Operators in C

Prefix and Postfix Decrement Operators

Prefix and postfix decrement operators are similar to their increment counterparts but perform the opposite operation. They are used to decrease the value of a variable by one in C programming.

With the prefix decrement operator (–num), the variable is decremented first, and then its updated value is used in the expression. For instance, if ‘num’ is 5 and we use ‘–num’, it becomes 4 immediately, and the expression using ‘num’ will utilize this updated value.

In contrast, with the postfix decrement operator (num–), the current value of the variable is used in the expression first, then decremented. So, if ‘num’ is 5 and we use ‘num–‘, the expression will use 5, and after the expression is evaluated, ‘num’ will be decremented to 4.

bottom of page