Introduction to Functions and Classes in
C++
Course Overview
This lecture introduces fundamental concepts in C++ programming, focusing
on functions and classes. We will cover:
• Function prototypes, inline functions, and default arguments
• Function declaration, definition, and calling
• Function overloading
• Introduction to classes and objects
• Access specifiers (public, private, protected)
• Member functions and data members
• The this pointer
Each concept will be explained with detailed examples to ensure clarity.
1. Function Prototypes, Inline
Functions, and Default Arguments
1.1 Function Prototypes
A function prototype declares a function before its definition, allowing the compiler
to know the function's signature (return type, name, and parameters) before it is
called.
Syntax:
cpp
Copy
returnType functionName(parameters);
1
Example:
cpp
#include <iostream>
using namespace std;
// Function prototype
int add(int a, int b);
int main() {
int result = add(5, 3); // Calling the function
cout << "Sum: " << result << endl;
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Explanation:
• The prototype int add(int a, int b); tells the compiler that add exists.
• The function is defined later and called in main().
1.2 Inline Functions
An inline function is a function that is expanded at the call site (like a macro) to
reduce function call overhead. Best for small, frequently called functions.
Syntax:
cpp
inline returnType functionName(parameters) {
// body
}
Example:cpp
#include <iostream>
using namespace std;
inline int square(int x) {
return x * x;
}
int main() {
cout << "Square of 5: " << square(5) << endl; // Expands to 5 * 5
return 0;
}
2
Explanation:
• The compiler replaces square(5) with 5 * 5 directly, avoiding function call
overhead.
1.3 Default Arguments
Default arguments allow a function to be called with fewer arguments than specified,
using default values for missing parameters.
Syntax:
cpp
returnType functionName(type param1 = defaultValue, ...);
Example:
cpp
#include <iostream>
using namespace std;
void display(string message = "Hello, World!") {
cout << message << endl;
}
int main() {
display(); // Uses default: "Hello, World!"
display("Hi!"); // Overrides default: "Hi!"
return 0;
}
Explanation:
• If no argument is passed, message takes the default value "Hello, World!".
3
2. Functions: Declaration, Definition,
and Calling
2.1 Function Declaration (Prototype)
• Tells the compiler about a function’s existence.
• Must include return type, name, and parameters.
2.2 Function Definition
• Contains the actual implementation.
2.3 Function Calling
• Invokes the function with arguments.
Example:
cpp
#include <iostream>
using namespace std;
// Declaration
int multiply(int a, int b);
int main() {
int result = multiply(4, 5); // Calling
cout << "Product: " << result << endl;
return 0;
}
// Definition
int multiply(int a, int b) {
return a * b;
}
4
3. Function Overloading
Function overloading allows multiple functions with the same name but different
parameters (type, number, or order).
Example:
cpp
#include <iostream>
using namespace std;
// Overloaded functions
void print(int num) {
cout << "Integer: " << num << endl;
}
void print(double num) {
cout << "Double: " << num << endl;
}
void print(string text) {
cout << "String: " << text << endl;
}
int main() {
print(10); // Calls print(int)
print(3.14); // Calls print(double)
print("Hello"); // Calls print(string)
return 0;
}
Explanation:
• The correct function is selected based on argument types.
5
4. Introduction to Classes and Objects
A class is a blueprint for creating objects (instances). It encapsulates data
(attributes) and functions (methods).
Syntax:
cpp
class ClassName {
// Data members (variables)
// Member functions (methods)
};
Example
cpp
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car car1; // Creating an object
car1.brand = "Toyota";
car1.year = 2020;
car1.display(); // Output: Brand: Toyota, Year: 2020
return 0;
}
6
5. Access Specifiers (Encapsulation)
Encapsulation restricts direct access to data members using:
• public: Accessible everywhere.
• private: Accessible only within the class (default).
• protected: Accessible within the class and derived classes.
Example:
cpp
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance; // Hidden from outside
public:
void deposit(double amount) {
if (amount > 0) balance += amount;
}
double getBalance() {
return balance;
}
};
int main() {
BankAccount acc;
acc.deposit(1000);
cout << "Balance: " << acc.getBalance() << endl;
// acc.balance = 5000; // Error: 'balance' is private
return 0;
}
7
6. Member Functions and Data
Members
• Data members: Variables inside a class.
• Member functions: Functions inside a class.
Example:
cpp
#include <iostream>
using namespace std;
class Rectangle {
private:
int length, width;
public:
void setDimensions(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
};
int main() {
Rectangle rect;
rect.setDimensions(5, 4);
cout << "Area: " << rect.area() << endl; // Output: 20
return 0;
}
8
7. The this Pointer
The this pointer refers to the current object inside a member function.
Example:
cpp
#include <iostream>
using namespace std;
class Person {
private:
string name;
public:
void setName(string name) {
this->name = name; // 'this' resolves name conflict
}
void display() {
cout << "Name: " << this->name << endl;
}
};
int main() {
Person p;
p.setName("Alice");
p.display(); // Output: Name: Alice
return 0;
}
Explanation:
• this->name refers to the class member, while name is the parameter.
Summary
• Functions can be declared, defined, and called with prototypes, inline
expansion, and default arguments.
• Function overloading allows multiple definitions with different parameters.
• Classes encapsulate data and functions into objects.
• Access specifiers (public, private, protected) control visibility.
• Member functions operate on data members.
• The this pointer refers to the current object.
This foundation prepares you for advanced OOP concepts like inheritance and
polymorphism.
9