COMPUTER SCIENCE ACADEMY
INTRODUCTION TO C++
Key Components of a C++ Class
A class in C++ serves as a blueprint that binds data and functions together. Its structure
consists of several critical parts:
The class Keyword and Naming: To define a class, you must use the
class keyword followed by a name (e.g., Circle or Rectangle). The
class body is enclosed in curly braces and must end with a
semicolon.
Access Specifiers: Classes are typically divided into two sections to
enforce security and organization:
o Private Section: This is where data members (variables like
radius or length) are declared. These variables are "hidden"
and cannot be accessed directly from outside the class, such
as from the main function.
o Public Section: This section contains member functions (tasks
like getData, calculation, and display). Making functions public
allows the "outside world" (like the main function) to interact
with the class and perform operations on the private data.
Initialization Rules: You cannot initialize a variable directly inside the
class definition (e.g., float r = 5.0; is not allowed and will cause a
compiler error). Data must be assigned or read through member
functions.
Implementing Member Functions
The sources highlight two ways to define member functions:
1. Inside the Class: You can define the function logic directly within the
class body.
2. Outside the Class: You can declare the function inside the class and
define its logic outside using the scope resolution operator (::). For
example, Rectangle::getData() tells the compiler that the getData
function belongs to the Rectangle class.
The Role of main() and Objects
A program written with a class will not execute its logic unless a main function is present.
The main() function acts as the entry point and follows a bottom-up approach. To use the
class, you must:
Create an Object: An object is a variable of the class type (e.g.,
Circle c;).
Use the Dot Operator: You call the public member functions in a
specific sequence using the object name and a dot (e.g., c.getData(),
c.calculation(), c.display()).
Practical Examples Covered
The sources provide logic for several common programs:
, Area and Circumference of a Circle: Using formulas like π×r2 for area
and 2×π×r for circumference.
Area of a Rectangle: Reading length and width to calculate area.
Distance from Origin: Using the sqrt() function from the math library
to find the distance of a point (x,y) from (0,0).
This technique is essential because it prevents data alteration; by keeping data private, you
ensure it can only be modified through the controlled, predefined functions within the class.
Core Concepts of OOP in C++
The sources outline several fundamental concepts required to build programs using classes:
Class Structure: A class is defined using the class keyword and
typically contains two access specifiers: private and public.
o Private Section: This is where data members (variables) are
declared. These variables are hidden from the outside world
for security, meaning they cannot be accessed directly from
outside the class (e.g., from the main function).
o Public Section: This section contains member functions that
perform tasks like reading data, performing calculations, and
displaying results. These functions are accessible from outside
the class.
Objects and the Dot Operator: To execute the logic within a class,
you must create an object in the main function. Member functions are
then called in a specific sequence using the object name followed by
the dot operator (.).
Initialization Rules: Unlike standard variables in main, you cannot
initialize data members directly within the class declaration (e.g.,
float r = 5.0; is not allowed).
Scope Resolution Operator (::): When a member function is declared
inside a class but defined outside of it, you must use the scope
resolution operator to link the function to its class (e.g.,
Rectangle::getData()).
Practical Program Examples
The source provides step-by-step logic for three specific programs:
1. Circumference of a Circle: Uses a radius variable and three functions
(getData, calculation, display) to compute the result using the
formula 2×3.14×r.
2. Area of a Rectangle: Demonstrates how to handle multiple inputs
(length and width) and emphasizes the importance of using the scope
resolution operator if functions are defined outside the class body.
3. Distance from Origin: Uses the sqrt() function from the math library
to calculate the distance of a point (x,y) from (0,0) using the formula
x2+y2.
Structure of a C++ Class
A class is defined using the class keyword followed by a name, with the body enclosed in
curly braces and ending with a semicolon. It is typically divided into two access specifiers:
, Private Section: This is where data members (variables like radius or
length) are declared. These are hidden from the "outside world,"
meaning they cannot be accessed directly from the main() function.
Notably, you cannot initialize these variables directly within the class
declaration; they must be assigned values through functions.
Public Section: This section contains member functions (tasks like
reading data, performing calculations, or displaying results). These
functions are accessible from outside the class and serve as the
interface for interacting with the private data.
Defining Member Functions
Member functions can be implemented in two ways:
1. Inside the Class: The logic is written directly within the class body.
2. Outside the Class: The function is only declared inside the class, and
its logic is defined outside using the scope resolution operator (::).
This operator (e.g., Rectangle::getData()) tells the compiler which
class the function belongs to.
Objects and the main() Function
C++ OOP follows a bottom-up approach, where the main() function serves as the
program's entry point and is written after the class definition. To execute the class logic, you
must:
Create an Object: This is a variable of the class type (e.g., Circle c;).
Use the Dot Operator (.): Functions are called through the object in a
specific sequence (e.g., c.getData(), then c.calculation()).
Advanced Function Techniques
Functions with Arguments: You can pass values from the main()
function directly into a member function by defining parameters
(e.g., getData(int x, int y)).
Return Types: While many member functions use void (returning
nothing), they can be designed to return a value (like a calculation
result) back to main() using a specific data type and the return
keyword
EXAMPLE 1: AREA OF CIRCLE USING “CLASS”
This code follows the 12th-grade sequential programming logic described in the sources,
where data is kept private and tasks are divided into public member functions.
#include <iostream.h>
#include <conio.h>
// Class definition
class Circle {
private:
// Data members (hidden from outside)
float r, area;
public:
// Member function to read data
INTRODUCTION TO C++
Key Components of a C++ Class
A class in C++ serves as a blueprint that binds data and functions together. Its structure
consists of several critical parts:
The class Keyword and Naming: To define a class, you must use the
class keyword followed by a name (e.g., Circle or Rectangle). The
class body is enclosed in curly braces and must end with a
semicolon.
Access Specifiers: Classes are typically divided into two sections to
enforce security and organization:
o Private Section: This is where data members (variables like
radius or length) are declared. These variables are "hidden"
and cannot be accessed directly from outside the class, such
as from the main function.
o Public Section: This section contains member functions (tasks
like getData, calculation, and display). Making functions public
allows the "outside world" (like the main function) to interact
with the class and perform operations on the private data.
Initialization Rules: You cannot initialize a variable directly inside the
class definition (e.g., float r = 5.0; is not allowed and will cause a
compiler error). Data must be assigned or read through member
functions.
Implementing Member Functions
The sources highlight two ways to define member functions:
1. Inside the Class: You can define the function logic directly within the
class body.
2. Outside the Class: You can declare the function inside the class and
define its logic outside using the scope resolution operator (::). For
example, Rectangle::getData() tells the compiler that the getData
function belongs to the Rectangle class.
The Role of main() and Objects
A program written with a class will not execute its logic unless a main function is present.
The main() function acts as the entry point and follows a bottom-up approach. To use the
class, you must:
Create an Object: An object is a variable of the class type (e.g.,
Circle c;).
Use the Dot Operator: You call the public member functions in a
specific sequence using the object name and a dot (e.g., c.getData(),
c.calculation(), c.display()).
Practical Examples Covered
The sources provide logic for several common programs:
, Area and Circumference of a Circle: Using formulas like π×r2 for area
and 2×π×r for circumference.
Area of a Rectangle: Reading length and width to calculate area.
Distance from Origin: Using the sqrt() function from the math library
to find the distance of a point (x,y) from (0,0).
This technique is essential because it prevents data alteration; by keeping data private, you
ensure it can only be modified through the controlled, predefined functions within the class.
Core Concepts of OOP in C++
The sources outline several fundamental concepts required to build programs using classes:
Class Structure: A class is defined using the class keyword and
typically contains two access specifiers: private and public.
o Private Section: This is where data members (variables) are
declared. These variables are hidden from the outside world
for security, meaning they cannot be accessed directly from
outside the class (e.g., from the main function).
o Public Section: This section contains member functions that
perform tasks like reading data, performing calculations, and
displaying results. These functions are accessible from outside
the class.
Objects and the Dot Operator: To execute the logic within a class,
you must create an object in the main function. Member functions are
then called in a specific sequence using the object name followed by
the dot operator (.).
Initialization Rules: Unlike standard variables in main, you cannot
initialize data members directly within the class declaration (e.g.,
float r = 5.0; is not allowed).
Scope Resolution Operator (::): When a member function is declared
inside a class but defined outside of it, you must use the scope
resolution operator to link the function to its class (e.g.,
Rectangle::getData()).
Practical Program Examples
The source provides step-by-step logic for three specific programs:
1. Circumference of a Circle: Uses a radius variable and three functions
(getData, calculation, display) to compute the result using the
formula 2×3.14×r.
2. Area of a Rectangle: Demonstrates how to handle multiple inputs
(length and width) and emphasizes the importance of using the scope
resolution operator if functions are defined outside the class body.
3. Distance from Origin: Uses the sqrt() function from the math library
to calculate the distance of a point (x,y) from (0,0) using the formula
x2+y2.
Structure of a C++ Class
A class is defined using the class keyword followed by a name, with the body enclosed in
curly braces and ending with a semicolon. It is typically divided into two access specifiers:
, Private Section: This is where data members (variables like radius or
length) are declared. These are hidden from the "outside world,"
meaning they cannot be accessed directly from the main() function.
Notably, you cannot initialize these variables directly within the class
declaration; they must be assigned values through functions.
Public Section: This section contains member functions (tasks like
reading data, performing calculations, or displaying results). These
functions are accessible from outside the class and serve as the
interface for interacting with the private data.
Defining Member Functions
Member functions can be implemented in two ways:
1. Inside the Class: The logic is written directly within the class body.
2. Outside the Class: The function is only declared inside the class, and
its logic is defined outside using the scope resolution operator (::).
This operator (e.g., Rectangle::getData()) tells the compiler which
class the function belongs to.
Objects and the main() Function
C++ OOP follows a bottom-up approach, where the main() function serves as the
program's entry point and is written after the class definition. To execute the class logic, you
must:
Create an Object: This is a variable of the class type (e.g., Circle c;).
Use the Dot Operator (.): Functions are called through the object in a
specific sequence (e.g., c.getData(), then c.calculation()).
Advanced Function Techniques
Functions with Arguments: You can pass values from the main()
function directly into a member function by defining parameters
(e.g., getData(int x, int y)).
Return Types: While many member functions use void (returning
nothing), they can be designed to return a value (like a calculation
result) back to main() using a specific data type and the return
keyword
EXAMPLE 1: AREA OF CIRCLE USING “CLASS”
This code follows the 12th-grade sequential programming logic described in the sources,
where data is kept private and tasks are divided into public member functions.
#include <iostream.h>
#include <conio.h>
// Class definition
class Circle {
private:
// Data members (hidden from outside)
float r, area;
public:
// Member function to read data