Posts

Showing posts from July 10, 2022

Operator Overloading Types and Rules | C++ Programming

Operator Overloading It is the mechanism of giving special meanings to an operator. By overloading operators, we can give additional meanings to operators like +, *, -, <=, >=, etc. which by default are supposed to work only on standard data types like ints, and floats. In general, a = b + c; works only with basic types like ‘int’ and ‘float’, and attempting to apply it when a, b and c are objects of a user-defined class will cause complaints from the compiler. But, using overloading, we can make this statement legal even when a, b and c are user-defined types (objects). Even though the semantics of an operator can be expressed, we cannot change its syntax. When an operator is overloaded, its original meaning is not lost. The grammar rules defined by C++ that govern its use such as the number of operands, precedence, and associatively of the operator remain the same for overloaded operators.  We can overload (give additional meanings to) all the C++ operators except the follo

Friend Functions | C++ Programming

Image
Friend Functions Friend Function A function is said to be a friend function of a class if it can access the members (including private members) of the class even if it is not the member function of this class. In other words, a friend function is a non-member function that has access to the private members of the class. Characteristics of a friend function: A friend function can be either global or a member of some other class. Since the friend function is not a part of the class, it can be declared anywhere in the public, private, and protected sections of the class. It cannot be called by using the object of the class since it is not in the scope of the class. It is called a normal function without the help of any object. Unlike member functions, it cannot access member names directly. So, it has to use an object name and dot operator with each member name (like A.x) It, generally, takes objects as arguments. The concepts of data hiding and encapsulation dictate that private

Constant in C Programming | C Programming

Constants in C Programming Constants in C refer to fixed values that do not change during the execution of a program.  const  is the keyword and it’s used to declare constants. A constant value is one that does not change during the execution of a program. C supports several types of constants. Integer Constants Real Constants Single Character Constants String Constants A constant value is one that does not change during the execution of a program. C supports several types of constants. Integer Constants Real Constants Single Character Constants String Constants Integer Constants: An integer constant is a sequence of digits. There are 3 types of integers namely decimal integers, octal integers, and hexadecimal integers. Decimal Integers: Consists of a set of digits 0 to 9 preceded by an optional + or - sign. Spaces, commas, and non-digit characters are not permitted between digits. Examples of valid decimal integer constant are: 123 -31 0 562321 + 78 Some examples