CS201 Final Term Past paper 2025

Understanding Friend Functions in C++

In C++, friend functions play a crucial role in granting access to private data within a class. To declare a friend function, we simply place it within the class definition using the friend keyword. An interesting aspect of friend functions is that they can access private data members and private helper functions of the class, which usually remain hidden from other parts of the program.

A common question arises: should we declare the friend function within the public or private section of the class? The answer is straightforward since the friend declaration is so powerful, it isn’t impacted by its placement. Whether it’s in the public or private section, the friend status remains unchanged. It’s essential to remember that even though the prototype of a friend function is placed inside the class, its actual definition is always written outside of the class body. This reinforces the concept that a friend function is not truly a member of the class itself.

Exploring the Concept of References

Today’s discussion centers on references in C++. Grasping this topic is crucial for fully understanding the finer details of C++. In C++, a reference acts like an alternative name or label for a variable that already exists. In simpler terms, it serves as another name for a data item that already exists in memory.

To declare a reference, we use the & symbol. This can be confusing at first because the & symbol also represents the address-of operator in different contexts. However, in the case of references, it’s used to create a direct link to an existing variable. Unlike pointers, a reference must be initialized at the moment it’s declared. This means that a reference cannot exist in a “null” state or be unassigned it always refers to a valid piece of data.

Differences Between References and Pointers

Although references and pointers might seem similar at first glance, they have key differences. A reference acts more like a direct alias or synonym for a variable rather than a standalone entity. A reference must always be tied to a valid variable or data item right when it’s created. Unlike pointers, references cannot be left unassigned or null.

In contrast, pointers can exist independently and be reassigned to point to different data locations. Pointers can also hold a NULL value, indicating that they point to nothing at all. References, on the other hand, cannot be NULL and cannot be reassigned once initialized. For instance, if we create a reference called ref to an integer, ref will always refer to the same integer throughout its lifetime.

There’s one notable exception: when passing references as function arguments, the reference does not need to be initialized in the function declaration itself. The actual data that the reference will refer to is determined by the calling function when the call is made.

The Issue of Dangling References

While references offer powerful and convenient features, they come with a critical caveat: the risk of creating dangling references. A dangling reference occurs when a function returns a reference to a variable that no longer exists.

Consider a situation where a function declares a local variable, such as int x;, and then returns a reference to x. As soon as the function finishes executing, the local variable x goes out of scope and is destroyed. However, the reference to x is still passed to the calling function, leading to a situation where the reference now points to invalid memory. Pointers are more flexible as they can exist on their own and can be reassigned to different memory locations.

To avoid this issue, it’s best practice to ensure that functions returning references do so only for global or static variables, which persist beyond the lifetime of the function. Otherwise, returning references to local variables can introduce unpredictable bugs and unstable behavior in the program.

Structured Query Language (SQL)

In the business world, most programming tasks involve databases. Modern databases, such as Oracle and SQL Server, rely on a special kind of language known as SQL (Structured Query Language). SQL has become so significant in handling data that an official standard for it has been set by ANSI. Unlike traditional programming languages, where you must specify both what you want to achieve and how to do it (as in C or C++), SQL focuses only on what you need. It handles the details of how to accomplish the task itself. This difference is why SQL is known as a fourth-generation language, compared to third-generation languages like C or Java.

Truth Table

When working with complicated decision structures, evaluating logical expressions can get tricky. Sometimes it’s challenging to understand how different parts of a complex logical expression will be evaluated and what the final result will be. That’s where truth tables come in handy. A truth table helps you clearly see how different logical conditions interact by listing out all possible combinations of inputs and their corresponding outputs. Even when the logic seems too tangled to write out in code, using a truth table can make it much easier to analyze and understand. Later on, when we dive into topics like logic design (which involves hardware elements like chips and gates), we’ll see how these tables are essential.

Variables and Pointers

Beyond basic programming structures, variables and pointers are crucial concepts in coding. A variable is like a tag or a label that helps you keep track of a value stored somewhere in a computer’s memory. It’s similar to writing a name on a container to show what’s inside. This makes it easier to use and manipulate the value without having to worry about its specific location in the computer’s memory. There are many different types of variables that help you store and manage data in various ways. Pointers, on the other hand, hold memory addresses, making them especially powerful when dealing with dynamic data structures and memory management.

Conclusion

Gaining a solid understanding of friend functions and references in C++ is key to truly mastering this robust programming language. Friend functions, declared with the friend keyword, break through the usual boundaries of encapsulation to allow controlled access to private data and utility functions. While references provide a convenient way to create synonyms for variables, they must be initialized upon declaration and cannot be reassigned later.

The subtle distinctions between pointers and references often trip up beginners. Pointers can be reassigned and can exist in a NULL state, while references must always refer to a valid entity. This difference makes references safer in many scenarios but also requires careful consideration.

cs201 final term past papers 2025, cs201 final term past papers, cs201 final term notes