2 Basics Of C Programming

Object Oriented Programming · Unit 2 · 5 hrs

Basics of C++ programming

Exam-focused notes for Basics of C++ programming (Object Oriented Programming, CSC166): what the TU syllabus asks and how it has actually been tested, with 12 solved past questions from this unit.

What this unit covers

  • C++ Program Structure
  • Character Set and Tokens
  • Data Type
  • Type Conversion
  • Preprocessor Directives
  • Namespace
  • Input/Output Streams and Manipulators
  • Dynamic Memory Allocation with new and delete
  • Control Statements
  • Functions: Function Overloading, Inline Functions, Default Argument, Pass by Reference, Return by Reference, Scope and Storage Class
  • Pointers: Pointer variables declaration & initialization, Operators in pointers, Pointers and Arrays, Pointer and Function

Functions

208010 marks

When inline functions may not work? Define and write syntax for default arguments. Write a program to display N number of characters by using default arguments for both parameters. Assume that the function takes two arguments, one character to be printed and other how many times the character to be printed respectively.[10]

--- The compiler may ignore the inline request and treat the function as a normal function in the following situations: 1. Function contains loops - If the inline function contains looping statements such as for, while, or do-while, the compiler will not in...

Full solved answer →
20795 marks

What is inline function? Why it is used? Write a program to illustrate inline function. [5]

An inline function is a C++ enhancement feature in which the compiler replaces the function call with the actual body (definition) of the function at compile time, instead of making a regular function call at runtime. To declare an inline function, the keyw...

Full solved answer →
20785 marks

What is meant by pass by reference? How can we pass arguments by reference by using reference variable? Illustrate with example. [5]

Pass by reference is a method of passing arguments to a function where the address (location) of a variable is passed to the function. When a function is called by reference, the formal arguments become references to the actual arguments. This means any cha...

Full solved answer →
20765 marks

What is meant by return by reference? How can we return values by reference by using reference variable? Illustrate with example. [5]

Return by reference means that a function returns a reference (alias) to a variable rather than returning a copy of its value. When a function returns by reference, the caller receives direct access to the original variable, and the returned reference can b...

Full solved answer →
20755 marks

What is the principle reason for using default arguments in the function? Explain how missing arguments and default arguments are handled by the function simultaneously? [5]

The principal reason for using default arguments in a function is to make function calls more flexible and convenient. A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the funct...

Full solved answer →
20755 marks

"An overloaded function appears to perform different activities depending the kind of data send to it." Justify the statement with appropriate example. [5]

Function overloading is a feature of C++ that allows two or more functions to have the same name but different parameter lists (different number, type, or order of arguments). The compiler distinguishes between overloaded functions based on the type and num...

Full solved answer →

Dynamic Memory Allocation with new and delete

20795 marks

How dynamic memory allocation is done using new and delete? Write program for illustrating use of new and delete. [5]

The process of allocating memory during runtime (execution time) of a program using the heap space of memory, to reduce wastage of memory, is called dynamic memory allocation. - new - memory allocation operator (allocates memory at runtime from heap) - dele...

Full solved answer →
20765 marks

What is use of new and delete operators? Illustrate with example. What are advantages of new over malloc. [5]

The new operator is used to dynamically allocate memory at runtime from the heap (free store). It allocates memory for a variable or object and returns a pointer to it. If allocation fails, it throws a badalloc exception. Syntax: The delete operator is used...

Full solved answer →

Input/Output Streams and Manipulators

20785 marks

Write short notes on: a. Manipulators b. Protected Access Specifier [5]

--- Manipulators are special functions used in C++ to format the output or manipulate the stream in a convenient way. They are used along with the insertion (<<) and extraction () operators. In the context of file access, each file has two associated pointe...

Full solved answer →
20765 marks

Write short notes on:- Cascading of IO operation - Pure Virtual Function [5]

--- Cascading of I/O operations refers to the technique of chaining multiple input or output operations together in a single statement using the insertion operator (<<) or extraction operator (). This is possible because in C++, the operators << and are ove...

Full solved answer →

Pointers

20755 marks

Write a member function called reverse() that reverses a string (an array of character). Use a for loop that swaps the first and last characters, then the second and next-to-last character and so on. The string should be passed to reverse() as an argument. [5]

A string in C++ is a character array. To reverse it, we swap the character at the first position with the character at the last position, then the second with the second-to-last, and so on, continuing until we reach the middle of the string. --- --- Suppose...

Full solved answer →

Type Conversion

2080.15 marks

Explain the significance of type conversion. How do we achieve dynamic memory allocation in C++? Explain with an example. [5]

--- Type conversion (typecasting) refers to changing an entity of one data type into another data type. It is used in computer programming to ensure variables are correctly processed by a function. - Ensures compatibility between different data types in an ...

Full solved answer →