Programming Methodologies

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which programming paradigm emphasizes organizing a program around data rather than code?

  • Structured programming
  • Assembly language programming
  • Procedural programming
  • Object-oriented programming (correct)

Which of the following is NOT a common trait of object-oriented programming languages?

  • Encapsulation
  • Polymorphism
  • Inheritance
  • Global variables (correct)

The mechanism that binds together code and the data it manipulates, keeping both safe from outside interference and misuse, is known as:

  • Encapsulation (correct)
  • Abstraction
  • Inheritance
  • Polymorphism

In the context of object-oriented programming, what does polymorphism enable?

<p>One interface to control access to a general class of actions (A)</p> Signup and view all the answers

Which concept supports classification by allowing one object to acquire the properties of another object?

<p>Inheritance (A)</p> Signup and view all the answers

In C++, what keyword is used to define a new object type?

<p>class (D)</p> Signup and view all the answers

Within a C++ class, what is the default access specifier for members?

<p>private (D)</p> Signup and view all the answers

What is the purpose of the public keyword in a C++ class definition?

<p>To declare members that are accessible from outside the class (D)</p> Signup and view all the answers

What is the name of the operator used to specify that a function is a member of a particular class?

<p>Scope resolution operator (::) (C)</p> Signup and view all the answers

What is the meaning of the term 'object' in C++?

<p>An instance of a class (A)</p> Signup and view all the answers

Which of the following best describes a class in object-oriented programming?

<p>A blueprint for creating objects (B)</p> Signup and view all the answers

What is meant by 'abstraction' in the context of software design?

<p>The separation of interface from implementation (C)</p> Signup and view all the answers

Why is abstraction considered beneficial in software development?

<p>It allows programmers to use code without understanding its underlying complexity (D)</p> Signup and view all the answers

What is the primary purpose of a constructor in C++?

<p>To initialize an object when it is created (A)</p> Signup and view all the answers

What distinguishes a constructor from other member functions in C++?

<p>Constructors have the same name as the class, while other member functions have different names (C)</p> Signup and view all the answers

What is a destructor's purpose in C++?

<p>To release the memory allocated to an object (D)</p> Signup and view all the answers

How is a destructor typically denoted in C++?

<p>With a tilde (~) preceding the class name (B)</p> Signup and view all the answers

Which type of constructor does not accept any arguments?

<p>Default constructor (B)</p> Signup and view all the answers

What is the primary role of a copy constructor?

<p>To copy the value of one object to another object of the same class (D)</p> Signup and view all the answers

What is the term for a class that inherits properties from another class?

<p>Derived class (C)</p> Signup and view all the answers

What fundamental concept is supported by inheritance?

<p>Classification (C)</p> Signup and view all the answers

In C++, what access specifier allows members of a base class to be accessible to derived classes but not directly accessible from outside the class hierarchy?

<p>protected (D)</p> Signup and view all the answers

What happens when a derived class uses 'public' inheritance?

<p>Public and protected members of the base class retain their access levels in the derived class. (B)</p> Signup and view all the answers

If a class Shape is privately inherited by a class Square, what is the access level of Shape's public members within Square?

<p>private (D)</p> Signup and view all the answers

Consider the following scenario: Class B inherits from Class A. Both classes have a member function called 'display()'. If an object of Class B calls 'display()', which version is executed in the absence of overriding?

<p>Class A's display() (D)</p> Signup and view all the answers

What access specifier should you use when inheriting a class to ensure that the public interface of the base class remains public in the derived class?

<p>public (C)</p> Signup and view all the answers

What is single inheritance?

<p>A class inherits from only one base class. (B)</p> Signup and view all the answers

What is multi-level inheritance?

<p>A class inherits from a derived class. (A)</p> Signup and view all the answers

In hierarchical inheritance, what is the relationship between derived classes?

<p>They all inherit from a single base class. (A)</p> Signup and view all the answers

What key feature enables C++ to achieve polymorphism?

<p>Function overloading (C)</p> Signup and view all the answers

What condition must be met for two or more functions in C++ to be considered overloaded?

<p>They must have the same name but different parameter lists. (A)</p> Signup and view all the answers

If a derived class redefines a member function that it inherited from a base class, what is this called?

<p>Function overriding (C)</p> Signup and view all the answers

What primarily differentiates function overriding from function overloading?

<p>Overriding requires inheritance, while overloading does not. (A)</p> Signup and view all the answers

What is the purpose of the address of operator (&) in C++?

<p>To return the memory address of a variable (C)</p> Signup and view all the answers

What is the role of the indirection operator (*) in C++?

<p>To access the value stored at a memory address held by a pointer (A)</p> Signup and view all the answers

Which operator is used to allocate memory at runtime in C++?

<p>new (A)</p> Signup and view all the answers

What is the purpose of the delete operator in C++?

<p>To release dynamically allocated memory (B)</p> Signup and view all the answers

What is the correct syntax to deallocate an array of integers pointed to by int* arr?

<p>delete[] arr; (B)</p> Signup and view all the answers

Flashcards

Object-Oriented Programming

A programming approach centered around data and objects, with data controlling access to code.

Encapsulation

Bundling code and data together, protecting them from outside interference and misuse.

Polymorphism

The characteristic that allows one interface to control access to a general class of actions.

Inheritance

The process by which one object acquires the properties of another object, supporting classification.

Signup and view all the flashcards

Class

The general form used to define an object in C++

Signup and view all the flashcards

Public

Variables that may be accessed by any function that is part of the class.

Signup and view all the flashcards

Private

Variables that may only be accessed by functions that are members of the class.

Signup and view all the flashcards

Member functions

Functions that are part of a class.

Signup and view all the flashcards

Member variables

Variables that belong to a class.

Signup and view all the flashcards

Instance

An object of a class in just the same way that some varibale is an instance of the int data type.

Signup and view all the flashcards

Scope resolution operator

It tells the compiler which class the function belongs to.

Signup and view all the flashcards

Constructor

Function that is automatically invoked when an object of a class is created.

Signup and view all the flashcards

Destructor

A special function that is a member of a class and has the same name as that class, but is preceded by a ~

Signup and view all the flashcards

Default constructor

A constructor that which doesn't take any argument.

Signup and view all the flashcards

Parameterized constructor

Constructor that is called where it is possible to pass arguments to constructors.

Signup and view all the flashcards

Copy constructor

A member function which initializes an object using another object of the same class.

Signup and view all the flashcards

Constructor Overloading

We can have more than one constructor in a class and it

Signup and view all the flashcards

Inheritance

One of the major traits of an object oriented programming language.

Signup and view all the flashcards

Multiple inheritance

Aclass can be derived from more than one classes,

Signup and view all the flashcards

Multi-level Inheritance

You can derive a class from the base class but you can also derive a class from the derived class

Signup and view all the flashcards

Hierarchical Inheritance

more than one derived class is created from a single base class

Signup and view all the flashcards

Function Overloading

two or more functions can share the same name as long as their parameter declarations are different

Signup and view all the flashcards

Function Overriding

we redefine the member function of a class which it inherited from its base class

Signup and view all the flashcards

Pointer

a variable that contains the address of another variable

Signup and view all the flashcards

Address of Operator &

operator that returns memory address of its operand

Signup and view all the flashcards

The Indirection Operator *

returns the value of the variable located at the address specified by its operand

Signup and view all the flashcards

Dynamic Memory

allocate memory at run time for the variable of a given type

Signup and view all the flashcards

delete

de-allocates memory that was previously allocated

Signup and view all the flashcards

Study Notes

Introduction

  • Programming methodologies have changed dramatically since the invention of the computer to accommodate the increasing complexity of programs.
  • Computers were first programmed by toggling binary machine instructions using the computer's front panel.
  • This approach worked as long as programs were just a few hundred instructions long.
  • As programs grew, assembly language was invented, enabling programmers to use symbolic representations of machine instructions and deal with larger, complex programs.
  • High-level languages were introduced as programs continued to grow, giving programmers more tools to handle complexity.
  • FORTRAN was the first widespread language and a very impressive first step; however, it is not a language that encourages clear, understandable programs.
  • Structured programming emerged in the 1960s and is encouraged by languages such as C and Pascal.
  • Structured languages made it possible to write moderately complex programs fairly easily.
  • Object-oriented methods were created to help programmers break through barriers as many projects neared (or exceeded) the point where the structured approach no longer worked.
  • Object-oriented programming combined the best ideas of structured programming with several new concepts, resulting in a fundamentally different way of organizing a program.

Program Organization

  • A program can be organized around its code (what is happening) or around its data (who is being affected)
  • Programs are typically organized around code when using only structured programming techniques and are regarded as "code acting on data”.
  • A program written in a structured language (like C) is defined by its functions, any of which may operate on any type of data used by the program.
  • Object-oriented programs are organized around data, with the key principle being "data controlling access to code."
  • In an object-oriented language, data and the routines permitted to act on that data are defined.
  • A data type defines precisely what sort of operations can be applied to that data.
  • All OOP languages have three traits in common to support the principles of object-oriented programming: encapsulation, polymorphism, and inheritance.

Encapsulation

  • Encapsulation is the mechanisms that binds together code and the data and keeps both safe from outside interference and misuse.
  • Code and data may be combined, creating a self-contained "black box" in an object-oriented language.
  • An object is created when code and data are linked in this fashion and supports encapsulation.
  • Code, data, or both may be private or public within an object.
  • Private code/data can only by another part of the object, meaning is not accessible by to code outside the object.
  • Public code/data may be accessed by other parts of your program, meaning it despite being defined within an object.
  • The public parts of an object are typically used to provide a controlled interface to the private elements of the object.
  • An object is essentially a variable of a user-defined type and each time you define a new type of object, you create a new data type
  • Each specific instance of this data type is a compound variable.

Polymorphism

  • Object-oriented programming languages support polymorphism, which is characterized by the phrase "one interface, multiple methods.”
  • Polymorphism is defined as the attribute that interface to control access to a general class of actions
  • The specific action selected is determined by the exact nature of the situation.
  • An example of polymorphism is a thermostat because no matter what type of furnace your house has (gas, oil, electric, etc.), the thermostat works the same way.
  • The thermostat (the interface) is the same, no matter what type of furnace (method) you have.
  • This concept can similarly apply to programming and used to define multiple types of stacks (integer, character, or floating-point values).
  • With polymorphism, you can define one set of names like push() and pop(), that can be used for all three stacks.
  • The compiler will automatically select the right function based upon the data being stored.
  • The interface to a stack (the functions push() and pop()) is the same no matter what type of stack is being used.
  • Polymorphism reduces complexity allowing the same interface to be used to access a general class of actions
  • Selecting the specific action is the compiler's job, so developers only utilize the general interface.

Inheritance

  • Inheritance is defined as process by which one object can acquire the properties of another object.
  • Inheritance is important because it supports classification and makes knowledge manageable by hierarchical classifications.
  • When objects define qualities that distinguish them within their class, it is made possible through inheritance.
  • Inheritance makes it that one object is a specific instance of a more general case
  • Inheritance is important to object-oriented programming.

Introducing C++ Classes

  • You have to define general for by using the class keyword to create an object.
  • Example:
class stack {
  int stck[SIZE];
  int tos;
  public:
    void init();
    void push(int i)
    int pop();
};
  • A class may contain private and public parts
  • All items defined are private by default
  • The variables stck and tos and cannot be accessed by any function
  • encapsulation can achieved through private items, and is a technique to tightly controlled access to certain items of data.
  • Private functions can also be defined within an object
  • Parts of a class can made public by declaring them after the public keyword
  • All functions and variables defined after public can be accessed by all other functions in the program.
  • Good practice dictates is making all data private and controlling access to it through public functions, even through there can also be public variables.
  • The functions init(), push(), and pop() are member functions because they are part of the class stack
  • The variables stck and tos are called member variables (or data members)
  • An Object forms when code and data member are with their class, such as stack
  • New data type specifier is made when defined a class, and using the class anem, you can create object that type
  • Creating an object:
  • stack mystack;
  • An instance of a class is created when you declare an object

Class Declaration

  • General form:
class class-name {
  private data and functions
public:
  public data and functions
} object name list;
  • Inside the declaration of stack, member functions were identified using their prototypes
  • All functions must be prototyped in C++
  • To code a function that is the member of a class, you mus tell the compiler with class the function belongs to by qualifying its name
void stack::push(int i)
{
  if(tos==SIZE) {
  cout << "Stack is full.\n";
  return;
  }
  stck[tos] = i;
  tos++;
}
  • Scope resolution operator: ::
  • If you refer to to a member of a class from code that is not within the class, then you must dot operator and the object name or that class
  • stack1.init();

Classes and Objetcs

  • A mechanism that combines the code and data into a single unit is called a class
  • Specification of the attributes and behaviour of an object:
class human {
  public:
    char name[20];
    int age;
};
  • An Object is an instantiation of a class, that can assume a property from class
  • Example:
#include <iostream.h>
class human 
{
  public:
    char name[20];
    int age;
};
int main () 
{
  human A;
  cout<<" Enter Name and Age: "<<endl;
  cin.getline (A.name, 20);
  cin>>A.age;
  cout <<” Name is "<< A.name << endl;
  cout <<" Age is "<< A.age << endl;
  return 0;
}
  • Behaviours (member functions, operations) = actions that object can perform

C++ Access specifiers

  • public, private,protected are known as reserved words and member access specifiers
  • Access specifiers:
  • public = accessible to outside class
  • private = cannot accessed outside class
  • protected = cannot be access outside the class, but can with inhereted classes

Abstraction

  • Similair software and can be withour know the implementation.
  • Example:
  • header file , where sqrt() is declared without knowing the algorithm is used.
  • Interface is spliting the implementation and implementaion is teh code youre writing
  • In C++, a classes properties and behaviours can be public, protected, or private.

Constructors and Destructors

  • Requiring objects to initialize before it is used
  • Before stack is used, the call to tos, must equal 0.
  • Requirement for initializing C++ allows objects to initialize once its implemented
  • This automatic initialization through a constructor function.
class stack 
{
    int stck[SIZE];
    int tos;
  public:
    stack(); // constructor
    void push(int i);
    int pop();
};
  • Has not type specificed
  • Function:
stack::stack()
{
  tos = 0;
  cout << "Stack Initialized\n";
}
  • Most constructor functions will just perform the initalizations, not output of input.

Types of Constructors

  • Default Constructors = does not take parameters, and doesn't take an argument
#include <iostream.h>
class construct {
  public:
  int a, b;
  // Default Constructor
    construct() {
      a = 10;
      b = 20;
    };
};
int main() {
  // Default constructor called automatically
  // when the object is created
  construct c;
  cout << "a: " << c.a << endl << "b: " << c.b;
  return 1;
}
  • Parameterized Constructors = to pass arguments
#include <iostream.h>
class Point {
  private:
    int x, y;
public:
  // Parameterized Constructor
  Point(int x1, int y1) {
    x = x1;
    y = y1;
  }
  int getX() {
    return x;
  }
  int getY() {
    return y;
  }
};
int main() {
  // Constructor called
  Point p1(10, 15);
  // Access values assigned by constructor
  cout << "p1.x = " << p1.getX() << ", p1.y = << p1.getY();
  return 0;
}
  • Copy Constructor = Initialize another object using other object

Inheritance

  • Object-oriented major language train, and C++ supports
  • Inheritance allows for hierachy of the classes to bult, shifting from more general to more specific
 class building {
    int rooms;
    int floors;
    int area;
  public:
    void set_rooms(int num);
     int get_rooms();
    void set_floors(int num);
    int get_floors();
    void set_area(int num);
    int get_area();
};
// house is derived from building
class house : public building {
  int bedrooms;
  int baths;
public:
  void set_bedrooms(int num);
  int get_bedrooms();
  void set_baths(int num);
  int get_baths();
};
  • Using public = Public members of the base class will become the public members of derived
  • Direct acess is accessed by to building.

Types of Inheritance

  • Single inheritance- derived calss as only one is called single
#include<iostream.h>
class Employee {
  int Id;
  char Name[25];
    int Age;
    long Salary;
public:
  void GetData() {
  cout<<"\n\tEnter Employee Id : ";
  cin>>Id;
        cin.ignore();
    cout<<"\n\tEnter EmployeeName : ";
    cin.getline(Name, 25);
    cout<<"\n\tEnter EmployeeAge : ";
    cin>>Age;
    cout<<"\n\tEnter Employee Salary : ";
    cin>>Salary;       }
    void PutData() {
     cout<<"\n\nEmployee Id : "<<Id;
       cout<<"\nEmployee Name : "<<Name;
       cout<<"\nEmployee Age : "<<Age;
       cout<<"\nEmployee Salary : "<<Salary;
}
};
class Company : public Employee 
{
  int RegNo;
  char CName[25];
public:
  void ReadData() {
  cout<<"\n\tEnter RegistrationNo. : ";
  cin>>RegNo;
   cin.ignore();
    cout<<"\n\tEnter CompanyName : ";
   cin.getline(CName,25);

        cin>>Cobj.c; }
";
"<<endl
  obj>>*c; }
";
}
};
  • Hierarchical inheritance - derived from class single

Polymorphism by Function Overloading

  • polymorphism in C++, two or more functions share teh names but different parameters.
  • Used when some functin related and has common name

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Evolution of Programming Methodologies
22 questions
Programming Methodologies Overview
10 questions
Programming Problem Solving Overview
37 questions
Use Quizgecko on...
Browser
Browser