Latest Activity In Study Groups

Join Your Study Groups

VU Past Papers, MCQs and More

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.

Object Oriented Programming (CS304)

Assignment # 03

Total marks = 20

                                                                                       Due Date =01/08/2016

Lectures Covered = All lectures up-to 33


Assignment Guidelines:


Instructions for the assignment preparation:


  • You are required to make a presentation file (.ppt or .pptx) for your assignment solution
  • Presentation will consist of maximum of 4 – 5 slides
  • First slide will contain student name and ID
  • Remaining slides will contain the solution of the assignment
  • You have to formally present your assignment with proper dress code and have to record the video of your presentation
  • You can use any video recording facility such as web cam, camera, mobile phone or etc. for recording of your presentation (See the tutorial “Tips for Recording Videos.mp4” available at download section of VULMS). Click to download video file.
  • In first slide, you will speak your name and ID, and in remaining slides, you will be required to explain your solution given in your presentation file.
  • Your introduction and solution explanation should not take more than 4 to 5 minutes
  • After recording of your presentation, you have to convert your video in “.3gp” format using any video conversion tool to reduce the size of the video file. (See the tutorial “Video Compression.mp4” available at download section of VULMS). Click to download video file.
  • After converting the video in .3gp format, review your file to verify that audio and video are properly playing. 
  • Install VLC media player to view your compressed video before uploading on VU-LMS.You can download VLC from
  • It is preferable to use English in your presentation in case of reluctance you can also use Urdu language.
  • Note that you will record your presentation standing in front of camera.


Weightage of Assignment:


  • This assignment has 5% weightage of your overall marks of this course.


Instructions for the assignment submission:


  • Save your assignment with your ID (e.g. bc020200786.rar)
  • Make sure that size of assignment file should not exceed 30 MB


For any query please contact:



Assignment Task

Note: You are given two topics. You have to select any one topic for your presentation. Instructions for solving the assignment are given at the top.


Topic-1:  Polymorphism

You need to explain polymorphism in detail. A working C++ code example will also be required if you select this topic to ensure that the audience gets the clear concept of this topic.


Topic-2: Generic Programming

In this topic you are required to explain that why and how overloaded functions can be changed to single function template. You need to explain this topic with the help of C++ code.

Views: 4932


Replies to This Discussion

Please Discuss here about this assignment.Thanks
Our main purpose here discussion not just Solution
We are here with you hands in hands to facilitate your learning and do not appreciate the idea of copying or replicating solutions.

good job SK(MS-SE)  

Thank you so much for this...

i have done my assignment no 2 from your video's ..

it's so helpful ...

dear students please all of u must listen this video and solve your assignment by yourself...

hmmm bilkul ..

brother very good for us! 

brother can u help me in C++ code???


listen this video completely you can get clear concept.

i listen all but in code i face problem

yes sure ..

Generic Programming


Generic programming refers to programs containing generic abstractions (general

code that is same in logic for all data types like printArray function), then we

instantiate that generic program abstraction (function, class) for a particular data

type, such abstractions can work with many different types of data.



As you can imagine major benefits of this approach are,

a. Reusability: Code can work for all data types

b. Writability: Code takes lesser time to write

c.    Maintainability: Code is easy to maintain as changes are needed to be made

in a single function or class instead of many functions or classes.



In C++ generic programming is done using templates.


Templates are of two kinds,


a. Function Templates (in case we want to write general function like


b. Class Templates (in case we want to write general class like Array class)


Compiler generates different type-specific copies from a single template

This concept is similar to concept of making prototype in the form of class for all

objects of same kind.

Function Templates


A function template can be parameterized to operate on different types of data types.




We write template keyword above any function make any function as template

function, they can be declared in any one of the following ways,


template/b> class T >

void funName( T x );


// OR


template/b> typename T >

void funName( T x );


// OR


template/b> class T, class U,  >

void funName( T x, U y,  );


Note here T is typename of class name and we use this T instead of data type/s for

which we want our function to work as template.


For Example – Function Templates

Following function template prints an array having almost any type of elements

(note the use of T instead of int or float or char in implementation of function):


template/b> typename T >

void printArray( T* array, int size )


for ( int i = 0; i /b> size; i++ )

cout  array[ i ]  “, ”; // Here data type of array is T



Similarly we can also write same function as,


template/b> class T >

void printArray( T* array, int size )


for ( int i = 0; i /b> size; i++ )

cout  array[ i ]  “, ”; // Here data type of array is T



There is no difference in two implementations.


Template function will be instantiated for a particular data type according to passed

argument as shown below,


int main() {

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

void printArray( iArray, 5 ); // Instantiated for int[] as passed array is of

type int []


cHar cArray[3] = { ‘a’, ‘b’, ‘c’ };

void printArray( cArray, 3 ); // Instantiated for cHar[] as argument is of type

cHar []

return 0;


Overloading vs. Templates


Function templates are used when we want to have exactly identical operations on

different data types in case of function templates we can not change implementation

from data type to data type however we can specialize implementation for a

particular data type.

In case we want to have similar operations on different data types we need function

overloading in function overloading case we can give similar but slightly different

implementation for each data type.

Example: Overloading vs. Templates


•      ‘+’ operation is overloaded for different operand types (different

implementation in each case)

•      A single function template can calculate sum of array of many types


Function Overloading

String operator +( const String& x,         const String& y ) {

String tmp;

tmp.pStr = new char[strlen(x.pStr) + strlen(y.pStr) + 1 ];

strcpy( tmp.pStr, x.pStr );

strcat( tmp.pStr, y.pStr );

return tmp;



String operator +( const char * str1, const String& y ) {

String tmp;

tmp.pStr = new char[ strlen(str1) + strlen(y.pStr) + 1 ];

strcpy( tmp.pStr, str1 );

strcat( tmp.pStr, y.pStr );

return tmp;




template< class T >

T sum( T* array, int size ) {

T sum = 0;


for (int i = 0; i < size; i++)

sum = sum + array[i];


return sum;


Template Arguments as Policy:


We can change behaviour of a template using template parameter. We can pass a template

argument to enforce some rule (policy). For example see the problem statement below,


“Write a function tHat compares two given cHaracter strings”


This function is similar to built in string comparison function strcmp with the

difference that it can perform both case sensitive and case insensitive comparisons

thanks :)

Cs304 first topic solution


Cs304 second topic solution



© 2021   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service