C++ Fundamentals: Pre-Defined Functions

BrainiestObsidian avatar
BrainiestObsidian
·
·
Download

Start Quiz

Study Flashcards

10 Questions

What is a function in C++?

A function is a block of organized, reusable code that is used to perform a single, related action.

What is the purpose of a pre-defined function in C++?

The purpose of a pre-defined function in C++ is to provide a library of functions that can be used in programs without the need to redefine them.

How do you call a function in C++?

To call a function in C++, you simply use its name and pass any required parameters into it.

What library is the 'sqrt()' function from?

cmath

What does the 'result = sqrt(number);' statement do?

The statement calculates the square root of the 'number' variable and assigns the result to the 'result' variable.

What is a class in C++?

A blueprint that defines a datatype by bundling data and methods

Encapsulation in C++ refers to the bundling of data and methods into a class.

True

What is the purpose of inheritance in C++?

Inheritance allows a new class (derived class) to inherit properties and behaviors (methods) from an existing class (base class).

A class defaults to ______ access control in C++.

private

Match the following terms with their definitions:

Polymorphism = Means 'many shapes' and allows objects of different classes to be treated as objects of a common superclass. Method Overloading = Using the same method name with different parameters. Method Overriding = Using the same method name and parameters in both superclass and subclass, where the subclass method overrides the superclass method.

Study Notes

Functions in C++

  • A function is a block of organized, reusable code that performs a single, related action.
  • Functions operate on inputs and produce results when called.

Pre-Defined Functions

  • C++ comes with libraries of predefined functions that can be used in programs.
  • Example: sqrt() function, found in the cmath library.
  • Predefined functions can be used to perform specific tasks.

Defining and Invoking Functions

  • Functions can take zero, one, or more arguments.
  • Functions can return only one value.
  • Function syntax: return-type function_name(type value1, type value2).
  • To invoke a function, call its name and pass in parameters if it takes arguments.

Example Program

  • To use predefined functions, include the necessary libraries (#include ).
  • Use the using namespace std; directive to access standard library functions.
  • In the main() function, declare variables and use functions to perform calculations.
  • Example: calculate the square root of a number using sqrt().

Classes and Objects

  • A class is a user-defined data type that holds its own data members and member functions.
  • Classes are essential for Object-Oriented Programming (OOP) in C++.

Class Definition

  • Class names should start with an uppercase letter, and multiple-word names should have the first letter of each word capitalized.
  • Classes contain data members and member functions, with access controlled by access specifiers.

Access Specifiers

  • Public: accessible from anywhere
  • Private: accessible only within the class where it is defined
  • Protected: accessible within the class and its subclasses

Inheritance

  • Inheritance allows a derived class to inherit properties and behaviors from a base class.
  • It promotes reusability and establishes a relationship between the base and derived classes.
  • Parent class (base class or superclass) and child class (subclass or derived class)

Polymorphism

  • Polymorphism means "many shapes" and allows objects of different classes to be treated as objects of a common superclass.
  • Method Overloading: same method name, different parameters
  • Method Overriding: same method name and parameters in the superclass and subclass

UML Class Diagram

  • A visual representation of the structure and relationships among classes in an object-oriented system.
  • Provides a high-level overview of classes, attributes, methods, and associations.
  • Consists of three sections: public, private, and protected.

Constructors and Destructors

  • Constructors are special class functions used to initialize objects.
  • Destructors are special class member functions that release memory reserved by an object when it goes out of scope.

Objects of Classes

  • Objects are instances of a class, holding data variables and member functions.
  • Each object has separate copies of data members and is initialized using constructors.
  • Destructor is called when an object goes out of scope, releasing memory.

Learn about pre-defined functions in C++, how they work, and how to use them in your programs. Understand the concept of functions, their types, and how they can make your code more efficient.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser