You are on page 1of 12

EDWARD MACHARIA MWANGI

NAC/9/852/22
MODULE II
OBJECT ORIENTED PROGRAMMING

STATIC OBJECTS.
A static object in C++ refers to a variable that retains its value and scope throughout the entire
program's execution. It is created when the program starts and destroyed when the program ends.
The scope of a static variable is limited to the block where it is defined, but it retains its value
between function calls.
Static variables are stored in a special area of memory distinct from the stack or heap. They are
initialized to zero or a null value for pointers.
Static variables are only visible within the translation unit (source file) where they are declared
and are not accessible from other files. The `static` keyword is used to define static variables
both inside functions (static local variables) and within classes (static class members).
Here is an example of the program using static objects:
#include <iostream>
using namespace std;

class Example {
public:
static int staticVariable; // Static variable shared among all instances
int regularVariable; // Non-static variable

Example() {
regularVariable = 0;
staticVariable++; // Increment static variable on each object creation
}
};

int Example::staticVariable = 0; // Initialize the static variable

int main() {
Example obj1;
Example obj2;

cout << "Static variable value: " << Example::staticVariable;


cout << "Object 1 regular variable: " << obj1.regularVariable;
cout << "Object 2 regular variable: " << obj2.regularVariable;

return 0;
}

IMPLICIT POINTER.
In C++, implicit pointers refer to objects of class types that can be used like pointers without
explicit dereferencing. This behavior is enabled by overloading the `->` operator in the class.
When an object of such a class is accessed with `->`, it behaves as if it were a pointer, allowing
direct access to its members.
This allows for more intuitive interaction with objects that represent dynamic memory or
complex data structures.
Keep in mind that this behavior is specific to class types and doesn't apply to basic types like
`int`, `char`, etc.
Here is an illustration:
#include <iostream>
using namespace std;

int main() {
int myNumber = 42;
int* myPointer = &myNumber; // Implicit conversion from int to int*

cout << "Value of myNumber: " << myNumber;


cout << "Address of myNumber: " << myPointer;

return 0;
}

INLINE FUNCTIONS.
In C++, an inline function is a function that, when called, is expanded directly at the call site
rather than undergoing the usual process of function call and return.
This can lead to potentially more efficient code execution, as it reduces the overhead of function
call.
Inline functions are declared with the `inline` keyword, suggesting to the compiler that it may
choose to inline the function.
However, this is a recommendation, and the compiler can choose to ignore it, especially for large
or complex functions.
Inline functions are most effective for small, frequently called functions. They are commonly
used for simple utility functions, accessors, or mutators.
It's important to note that the decision to inline a function ultimately rests with the compiler's
optimization strategy.
An Inline global function is a function that is defined in the global namespace (i.e., not within a
class or a namespace) and has been declared as inline. This means that, like any other inline
function, when called, it is expanded directly at the call site instead of going through the normal
process of function call and return.
An Inline Member is the same as an Inline global function.
Here's an example of an inline global function:
#include <iostream>
using namespace std;

// Declaration of an inline function


inline void greet() {
cout << "Hello, World!";
}

int main() {
greet(); // Function call, which may be expanded in-line

return 0;
}

FRIEND OF A CLASS.
In C++, a "friend" of a class is a function or another class that is granted special access to the
private and protected members of that class.

This access allows the friend function or class to operate on the private or protected data of the
class as if it were a member of that class.

Here are some key points about friends in C++:


1. Declaration: A friend function or class is declared within the class that wants to grant it
access. This declaration is usually done in the class's `private` or `protected` section.
2. Access Levels: Friends have access to all members (private and protected) of the class. This
access is not reciprocal, meaning the friend function or class doesn't automatically grant access
to its own members.
3. Declaration Outside the Class: The friend function or class must be declared or defined
outside the class where it is declared as a friend.
4.Friend Functions: These are regular functions that are declared as friends. They can be
standalone functions or member functions of another class.
5. Friend Classes: A friend class is a class that is granted access to the private and protected
members of another class.
Here is a program example:
#include <iostream>
using namespace std;

class MyClass {
private:
int privateMember;

public:
MyClass() : privateMember(0) {}

friend void friendFunction(MyClass& obj); // Declaration of friend function

void setPrivateMember(int value) {


privateMember = value;
}

void displayPrivateMember() {
cout << "Private Member: " << privateMember << endl;
}
};

// Definition of the friend function


void friendFunction(MyClass& obj) {
obj.privateMember = 42; // Accessing private member of MyClass
}

int main() {
MyClass obj;

obj.displayPrivateMember(); // Output: Private Member: 0

friendFunction(obj); // Call to friend function

obj.displayPrivateMember(); // Output: Private Member: 42

return 0;
}
STATIC CLASS MEMBER.
In C++, a static class member is a variable or a function that belongs to the class rather than to
any specific instance of the class. This means that all instances of the class share the same static
member.

For example, if you have a static variable in a class, changing its value in one instance will affect
the value in all other instances of the class. Similarly, a static function can be called without
creating an instance of the class.
Here's an example of a static variable and function in C++:
#include <iostream>
using namespace std;

class Example {
public:
static int staticVariable; // Declaration of a static variable

Example() {
staticVariable++; // Increment static variable on each object creation
}
};

int Example::staticVariable = 0; // Initialization of the static variable

int main() {
Example obj1;
Example obj2;
Example obj3;

cout << "Static variable value: " << Example::staticVariable;

return 0;
}
SPECIFIES.
CONST.>> In C++, the `const` keyword is used to specify that a variable or a member function
cannot be modified. It can be applied in different contexts:

1. For Variables:
- When used with a variable, `const` makes that variable immutable. It means its value cannot
be changed after initialization.

2. For Pointers:
- When `const` is used with a pointer, it indicates that the value it points to cannot be modified.

3. For Member Functions:


- When `const` is used after a member function declaration, it specifies that this function will
not modify any member variables of the class.

4. For Member Variables in Classes:


- When applied to a member variable, it means that the member variable cannot be modified
within any member function of the class.

5. For References:
- A `const` reference is a reference that is bound to a constant value and cannot be used to
modify that value.
Here’s a program:
#include <iostream>
using namespace std;

class MyClass {
public:
int regularVariable;
const int constantVariable;

MyClass(int reg, int con) : regularVariable(reg), constantVariable(con) {}

void display() const {


// regularVariable = 10; // Error: Cannot modify a const member function
cout << "Regular Variable: " << regularVariable << ", Constant Variable:
" << constantVariable ;
}
};

int main() {
MyClass obj(5, 10);

obj.display();

// obj.constantVariable = 20; // Error: Cannot modify a const member

return 0;
}
ENUM.>> In C++, an `enum` (enumeration) is a user-defined data type used to assign names to
integral constants. It allows you to create a set of named constants, making your code more
readable and easier to maintain. They provide a clear and meaningful way to represent these
states in your code.
Here's a brief explanation of how to use `enum` in C++:

#include <iostream>
using namespace std;

enum Day {
Sunday, // 0
Monday, // 1
Tuesday, // 2
Wednesday, // 3
Thursday, // 4
Friday, // 5
Saturday // 6
};

int main() {
Day today = Tuesday;

if (today == Sunday || today == Saturday) {


cout << "It's the weekend!";
} else {
cout << "It's a weekday.";
}

return 0;
}
Typedef.>> In C++, `typedef` is a keyword used to create an alias or a new name for an
existing data type. It provides a way to make complex type declarations more readable and easier
to manage. Another common use of `typedef` is with user-defined data types such as structures
or classes:
Here's a brief explanation of how to use `typedef` in C++:
#include <iostream>
using namespace std;

typedef int myInt; // 'myInt' is now an alias for 'int'

int main() {
myInt num = 42; // 'myInt' is essentially the same as 'int'

cout << "Value of num: " << num;

return 0;
}

ENUMERATED CONSTANTS.
Enumerated constants in C++ are a way to represent a set of named values, often referred to as
an enumeration. They provide a convenient and human-readable way to define and work with a
group of related constants.

Here's a brief explanation of how enumerated constants work in C++:


#include <iostream>
using namespace std;

enum Day {
Sunday, // 0
Monday, // 1
Tuesday, // 2
Wednesday, // 3
Thursday, // 4
Friday, // 5
Saturday // 6
};

int main() {
Day today = Tuesday;

switch(today) {
case Sunday:
cout << "It's Sunday!";
break;
case Monday:
cout << "It's Monday!";
break;
case Tuesday:
cout << "It's Tuesday!";
break;
case Wednesday:
cout << "It's Wednesday!";
break;
case Thursday:
cout << "It's Thursday!";
break;
case Friday:
cout << "It's Friday!" ;
break;
case Saturday:
cout << "It's Saturday!";
break;
default:
cout << "Invalid day!";
}

return 0;
}
POINTER TO MEMBERS.
In C++, a "pointer to member" is a special type of pointer that can be used to access non-static
class members (both variables and functions) through an instance of the class. This allows you to
indirectly reference class members and call member functions on objects.
Pointers to members are particularly useful in scenarios where you need to dynamically choose
which member to access or call, based on runtime conditions. They provide a way to work with
members of a class in a flexible and dynamic manner.
Here's a brief explanation of pointers to members in C++:
#include <iostream>
using namespace std;

class MyClass {
public:
int myVariable;
void myFunction(int arg) {
cout << "Argument: " << arg;
}
};

int main() {
MyClass obj;
MyClass* ptr = &obj;

int MyClass::*memberPtr = &MyClass::myVariable; // Pointer to a member


variable
void (MyClass::*functionPtr)(int) = &MyClass::myFunction; // Pointer to a
member function

obj.*memberPtr = 42; // Accessing member variable through an object


(obj.*functionPtr)(10); // Calling member function through an object

ptr->*memberPtr = 24; // Accessing member variable through a pointer


(ptr->*functionPtr)(5); // Calling member function through a pointer

return 0;
}

You might also like