Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming assignment, answer the following questions 1. What templates are

ID: 3845306 • Letter: C

Question

C++ Programming assignment, answer the following questions

1. What templates are and how to use them as a client (e.g. create a vector of strings and use it). Very basic information. You will NOT be asked to use a queue, set, or map but you should have some idea what they are.

2. What exceptions are and the very basics (try/catch/throw

Functions:

3.What is a pass by value and pass by reference?

4.How to write a function that takes object arguments (e.g. a Point object, or vector of Points) and uses the objects or modifies them in the function? How to return an object from a function (e.g. return a string, a Point or a vector of strings/Points)?

5.How to write a function that takes an array argument and uses the array or modifies it. Be comfortable with arrays of objects (e.g. an array of Point objects)?

Explanation / Answer

1.Templates --

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ Standard Library provides many useful functions within a framework of connected templates.

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

the C++ Standard Library contains the function template max(x, y) which returns the larger of x and y. That function template could be defined like this:

template <typename T>
inline T max(T a, T b) {
return a > b ? a : b;
}

Queue --

queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".

Set--

Sets are containers that store unique elements following a specific order.

In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

Map --

Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.

2.Exception handling --

An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.

throw: A program throws an exception when a problem shows up. This is done using a throw keyword.

catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

try
{
// protected code
}catch( ExceptionName e1 )
{
// catch block
}catch( ExceptionName e2 )
{
// catch block
}catch( ExceptionName eN )
{
// catch block
}


3.Call by value --

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
By default, C++ uses call by value to pass arguments. In general, this means that code within a function cannot alter the arguments used to call the function.

Call by refrence --

The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.

4.Passing the object--

#include <iostream>
using namespace std;

class Complex
{
private:
int real;
int imag;
public:
Complex(): real(0), imag(0) { }
void readData()
{
cout << "Enter real and imaginary number respectively:"<<endl;
cin >> real >> imag;
}
void addComplexNumbers(Complex comp1, Complex comp2)
{
real=comp1.real+comp2.real;

imag=comp1.imag+comp2.imag;
}

void displaySum()
{
cout << "Sum = " << real<< "+" << imag << "i";
}
};
int main()
{
Complex c1,c2,c3;

c1.readData();
c2.readData();

c3.addComplexNumbers(c1, c2);
c3.displaySum();

return 0;
}

Returning the object --

#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(): real(0), imag(0) { }
void readData()
{
cout << "Enter real and imaginary number respectively:"<<endl;
cin >> real >> imag;
}
Complex addComplexNumbers(Complex comp2)
{
Complex temp;

temp.real = real+comp2.real;

temp.imag = imag+comp2.imag;
return temp;
}
void displayData()
{
cout << "Sum = " << real << "+" << imag << "i";
}
};

int main()
{
Complex c1, c2, c3;

c1.readData();
c2.readData();

c3 = c1.addComplexNumbers(c2);

c3.displayData();
  
return 0;
}

5.Array modification --

#include <stdio.h>

#define ARRAY_SIZE 5

void changeArray ( int, int[] );
void changeElement ( int );

int main ( void )
{
int i, j;

int data[ ARRAY_SIZE ] = { 1, 2, 3, 4, 5 };

printf("Original array: ");

for ( i = 0; i < ARRAY_SIZE; i++ )
{
printf("%d ", data[ i ]);
}

changeArray ( ARRAY_SIZE, data );

printf(" Modified array ");

for ( j = 0; j < ARRAY_SIZE; j++ )
{
printf("%d ", data[ j ]);
}

printf(" Orginal array element data[ 3 ]: ");

printf("%d", data[ 3 ]);

changeElement ( data[ 3 ] );

printf(" Modified array element data[ 3 [: ");

printf("%d", data[ 3 ]);

getchar();

return 0;
}

void changeArray ( int size, int x[] )
{
int i;

for ( i = 0; i < size; i++ )
{
x[ i ] *= 2;
}
}

void changeElement ( int e )
{
printf("%d", ( e *= 2 ));
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote