Ass Introduction to Object Oriented Programming & C++

30
Amity Campus Uttar Pradesh India 201303 ASSIGNMENTS PROGRAM: BSc IT SEMESTER-IV Subject Name : Study COUNTRY : Roll Number (Reg.No.) : Student Name : INSTRUCTIONS a) Students are required to submit all three assignment sets. ASSIGNMENT DETAILS MARKS Assignment A Five Subjective Questions 10 Assignment B Three Subjective Questions + Case Study 10 Assignment C Objective or one line Questions 10 b) Total weightage given to these assignments is 30%. OR 30 Marks c) All assignments are to be completed as typed in word/pdf. d) All questions are required to be attempted.

Transcript of Ass Introduction to Object Oriented Programming & C++

Page 1: Ass Introduction to Object Oriented Programming & C++

Amity CampusUttar PradeshIndia 201303

ASSIGNMENTSPROGRAM: BSc IT

SEMESTER-IVSubject Name :Study COUNTRY :Roll Number (Reg.No.) :Student Name :

INSTRUCTIONSa) Students are required to submit all three assignment sets.

ASSIGNMENT DETAILS MARKSAssignment A Five Subjective Questions 10Assignment B Three Subjective Questions + Case Study 10Assignment C Objective or one line Questions 10

b) Total weightage given to these assignments is 30%. OR 30 Marksc) All assignments are to be completed as typed in word/pdf.d) All questions are required to be attempted.e) All the three assignments are to be completed by due dates and need to be

submitted for evaluation by Amity University.f) The students have to attached a scan signature in the form.

Signature : _________________________________Date : _________________________________

( √ ) Tick mark in front of the assignments submittedAssignment

‘A’Assignment ‘B’ Assignment ‘C’

Page 2: Ass Introduction to Object Oriented Programming & C++

Introduction to Object Oriented Programming & C++

Part A

Q1. Explain briefly characteristics of OOPS language and mention advantages of OOPS

approach over procedural programming language.

The main difference of OOP with procedural language are:a) Object Orientation Languages objective is to develop an application based on real time while Procedural Programming Languages(PPL) are more concerned with the processing of procedures or functions. b) In OOP, more emphasis is given on data rather than procedures, while the programs are divided into objects and the data is encapsulated (i.e. hidden) from the external environment, providing more security to data which is not generaly applicable or rather possible in PPL like C. c) In OOP, Objects communicate with each other via functions (will be discussed in next the units) while there is no explicit communication in PPL rather its simply a passing values to the arguments to the functions.d) OOP follows bottom up approach of program execution while in PPL its top down approach.e) OOP’s concepts includes Inheritance, Encapsulation and Data Abstraction, Polymorphism, Multithreading, and Message Passing while PPL is simply a programming in a traditional way of calling functions and returning values.PPL does not support it.

The list of OOP languages are :- C++, JAVA, VB.NET, C#.NET

The list of PPL languages:- C, VB, Perl, Basic, FORTRAN.

Page 3: Ass Introduction to Object Oriented Programming & C++

As we know OOP is basically used to solve the real life applications. We can call OOP as the

programming methodology that focuses on data rather than processes. It is a moduler approach

to computer program design. Each module or objects combines the data and procedures

(sequence of instructions) that act on the data. A group of objects that have the properties,

operations and behavior in common is called a class.

Characteristics of OOPS language :

a) Using inheritance concepts, we can reuse the existing classes i.e. program code or data structure. In

other words we can eliminate the redundant code.

Page 4: Ass Introduction to Object Oriented Programming & C++

b) Data hiding mechanism helps us to build the secure programs that can not be invaded by code in the

other parts of the program.

c) The data centered design approach helps to aquire more details of model in implementable form.

d) OOP’s can easily upgrade a small system to a large system with just a few modifications in the

previous existing code or data structure.

e) Message passing concepts for communication among objects makes the interface descriptions with

external system much simpler.

f) Using OOP concepts the time and space complexity can be reduced.

Q2. Give the tabular difference between structures, and classes.

The struct default access type is public. A struct should typically be used for grouping data.

The class default access type is private, and the default mode for inheritance is private. A class should be used for grouping data and methods that operate on that data.

In short, the convention is to use struct when the purpose is to group data, and use classes when we require data abstraction and, perhaps inheritance.

In C++ structures and classes are passed by value, unless explicitly de-referenced. In other languages classes and structures may have distinct semantics - ie. objects (instances of classes) may be passed by reference and structures may be passed by value.

Q3. What is the difference between keyword and identifier?

Identifier keyword

Identifiers are names that are given to

various program elements, such as

variable, function and arrays.

C++ take some reserved word called

keyword, they have predefine meaning in

C++.

Identifiers consist of letters and digits. Keyword consist only letter.

Identifier’s first character must be a letter. Keyword’s all character is letter.

Identifiers Upper and lowercase letter is

use.

Keywords are all lowercase.

Upper and lowercase are not equivalent. Upper and lowercase are also not

equivalent.

Like: X, sum_5, _weather etc.But 4th is not identifier cause identifier

Like: auto, short, long etc.

Page 5: Ass Introduction to Object Oriented Programming & C++

first character must a letter.

Q4. What is difference between fundamental and derived data types?

Q5. Differentiate between ‘call by reference’ and ‘call by value’

The call by reference method in place copies the values of actual parameters into the formal

parameters, a reference to the original variable is passed. (Reference is an alias (i.e. a different name)

for the predefined variable)That is, the same variable’s value can be accessed by any of the two

names. When a function is called by reference, then the formal parameters become references (or

alias) to the actual parameter in the calling function.

The call by value method copies the values of actual parameters into the formal parameters, that is,

the function creates its own copy of

argument values and then uses them.

Page 6: Ass Introduction to Object Oriented Programming & C++

Part B

Q1. Write a C++ program that invoke a function calc() which intake two integer and

an arithmetic operator and print the corresponding result..

Q2. Answer the questions (i) to (iii) based on the following code

class stationary{           char Type;           char Manufacturer [10];           public:           stationary();            void Read_sta_details( );            void Disp_sta_details( );};class office: public stationary{           int no_of_types;           float cost_of_sta;           public:           void Read_off_details( );            void Disp_off_details( );};class printer: private office {           int no_of_users;            char delivery_date[10];           public:           void Read_pri_details( );           void Disp_pri_details( );};void main ( ){           printer MyPrinter;            }

i. Mention the member names which are accessible by MyPrinter declared in main() function

ii. What is the size of MyPrinter in bytes?

Page 7: Ass Introduction to Object Oriented Programming & C++

iii. Mention the names of functions accessible from the member functionRead_pri_details () of class printer.

Q3. Develop a Project on any one using C++ :

a. Hospital Management System

#include<iostream.h> #include<conio.h> #include<string.h> #include<stdlib.h>

// define maximum number of patients in a queue #define MAXPATIENTS 100

// define structure for patient data struct patient { char FirstName[50]; char LastName[50]; char ID[20]; };

// define class for queue class queue { public: queue (void); int AddPatientAtEnd (patient p); int AddPatientAtBeginning (patient p); patient GetNextPatient (void); int RemoveDeadPatient (patient * p); void OutputList (void); char DepartmentName[50]; private: int NumberOfPatients; patient List[MAXPATIENTS]; };

// declare member functions for queue

queue::queue () {

Page 8: Ass Introduction to Object Oriented Programming & C++

// constructor NumberOfPatients = 0; }

int queue::AddPatientAtEnd (patient p) { // adds a normal patient to the end of the queue. // returns 1 if successful, 0 if queue is full. if (NumberOfPatients >= MAXPATIENTS) { // queue is full return 0; } // put in new patient else List[NumberOfPatients] = p; NumberOfPatients++; return 1; } int queue::AddPatientAtBeginning (patient p) { // adds a critically ill patient to the beginning of the queue. // returns 1 if successful, 0 if queue is full. int i; if (NumberOfPatients >= MAXPATIENTS) { // queue is full return 0; }

// move all patients one position back in queue for (i = NumberOfPatients-1; i >= 0; i--) { List[i+1] = List[i]; } // put in new patient List[0] = p; NumberOfPatients++; return 1; }

patient queue::GetNextPatient (void) { // gets the patient that is first in the queue. // returns patient with no ID if queue is empty

Page 9: Ass Introduction to Object Oriented Programming & C++

int i; patient p; if (NumberOfPatients == 0) { // queue is empty strcpy(p.ID,""); return p;} // get first patient p = List[0]; // move all remaining patients one position forward in queue NumberOfPatients--; for (i=0; i<NumberOfPatients; i++) { List[i] = List[i+1]; } // return patient return p; }

int queue::RemoveDeadPatient (patient * p) { // removes a patient from queue. // returns 1 if successful, 0 if patient not found int i, j, found = 0; // search for patient for (i=0; i<NumberOfPatients; i++) { if (stricmp(List[i].ID, p->ID) == 0) { // patient found in queue *p = List[i]; found = 1; // move all following patients one position forward in queue NumberOfPatients--; for (j=i; j<NumberOfPatients; j++) { List[j] = List[j+1]; } } } return found; }

void queue::OutputList (void) { // lists entire queue on screen int i;

Page 10: Ass Introduction to Object Oriented Programming & C++

if (NumberOfPatients == 0) { cout << "Queue is empty"; } else {

for (i=0; i<NumberOfPatients; i++) { cout << "" << List[i].FirstName; cout << " " << List[i].LastName; cout << " " << List[i].ID; } } }

// declare functions used by main:

patient InputPatient (void) { // this function asks user for patient data. patient p; cout << "Please enter data for new patient First name: "; cin.getline(p.FirstName, sizeof(p.FirstName)); cout << "Last name: "; cin.getline(p.LastName, sizeof(p.LastName)); cout << "Social security number: "; cin.getline(p.ID, sizeof(p.ID)); // check if data valid if (p.FirstName[0]==0 || p.LastName[0]==0 || p.ID[0]==0) { // rejected strcpy(p.ID,""); cout << "Error: Data not valid. Operation cancelled."; getch(); } return p; }

void OutputPatient (patient * p) { // this function outputs patient data to the screen if (p == NULL || p->ID[0]==0) { cout << "No patient"; return;

Page 11: Ass Introduction to Object Oriented Programming & C++

} else cout << "Patient data:"; cout << "First name: " << p->FirstName; cout << "Last name: " << p->LastName; cout << "Social security number: " << p->ID; }

int ReadNumber() { // this function reads an integer number from the keyboard. // it is used because input with cin >> doesn't work properly! char buffer[20]; cin.getline(buffer, sizeof(buffer)); return atoi(buffer); }

void DepartmentMenu (queue * q) { // this function defines the user interface with menu for one department int choice = 0, success; patient p; while (choice != 6) { // clear screen clrscr(); // print menu cout << "Welcome to department: " << q->DepartmentName; cout << "Please enter your choice:"; cout << "1: Add normal patient\n"; cout << "2: Add critically ill patient\n"; cout << "3: Take out patient for operation\n"; cout << "4: Remove dead patient from queue\n"; cout << "5: List queue\n"; cout << "6: Change department or exit\n"; // get user choice choice = ReadNumber(); // do indicated action switch (choice) { case 1: // Add normal patient p = InputPatient(); if (p.ID[0]) { success = q->AddPatientAtEnd(p);

Page 12: Ass Introduction to Object Oriented Programming & C++

clrscr(); if (success) { cout << "Patient added:";

} else { // error cout << "Error: The queue is full. Cannot add patient:"; } OutputPatient(&p); cout << "Press any key"; getch(); } break;

case 2: // Add critically ill patient p = InputPatient(); if (p.ID[0]) { success = q->AddPatientAtBeginning(p); clrscr(); if (success) { cout << "Patient added:"; } else { // error cout << "Error: The queue is full. Cannot add patient:"; }

OutputPatient(&p); cout << "Press any key"; getch(); } break;

case 3: // Take out patient for operation p = q->GetNextPatient(); clrscr(); if (p.ID[0]) { cout << "Patient to operate:"; OutputPatient(&p);}

Page 13: Ass Introduction to Object Oriented Programming & C++

else { cout << "There is no patient to operate."; } cout << "Press any key"; getch(); break;

case 4: // Remove dead patient from queue p = InputPatient(); if (p.ID[0]) { success = q->RemoveDeadPatient(&p); clrscr(); if (success) { cout << "Patient removed:"; } else { // error cout << "Error: Cannot find patient:"; } OutputPatient(&p); cout << "Press any key"; getch(); } break;

case 5: // List queue clrscr(); q->OutputList(); cout << "Press any key"; getch(); break; } } }

b. Railways Reservation System

c. Student Information System

Page 14: Ass Introduction to Object Oriented Programming & C++

Part C

Q1 Which of the following is false?

a.    Cout represents the standard output stream in c++.

b.    Cout is declared in the iostream standard file

c.    Cout is declared within the std namespace

d.    None of above

 

Q2. What is the only function all C++ programs must contain?

A. start()

B. system()

C. main()

D. program()

 

Q3. What punctuation is used to signal the beginning and end of code blocks?

A. { }

B. -> and <-

C. BEGIN and END

D. ( and )

 

Q4. What punctuation ends most lines of C++ code?

A. . (dot)

B. ; (semi-colon)

Page 15: Ass Introduction to Object Oriented Programming & C++

C. : (colon)

D. ' (single quote)

 

Q5. Which of the following is a correct comment?

A. */ Comments */

B. ** Comment **

C. /* Comment */

D. { Comment }

Q6. Which of the following is not a correct variable type?

A. float

B. real

C. int

D. double

Q7. Which of the following is the correct operator to compare two variables?

A. :=

B. =

C. equal

D. ==

Q8. Which of the following is true?

A. 1

B. 66

C. .1

D. -1

E. All of the above

 

Q9. Which of the following is the boolean operator for logical-and?

A. &

Page 16: Ass Introduction to Object Oriented Programming & C++

B. &&

C. |

D. |&

 

Q10. Evaluate !(1 && !(0 || 1)).

A. True

B. False

C. Unevaluatable

Q11. Every statement in C++ program should end with

a.    A full stop (.)

b.    A Comma (,)

c.    A Semicolon (;)

d.    A colon (:)

Q12. C++ was originally developed by

1. Nicolas Wirth

2. Donald Knuth

3. Bjarne Stroustrup

4. Ken Thompson

Q13. The standard c++ comment

1. /

Page 17: Ass Introduction to Object Oriented Programming & C++

2. //

3. /* and */

4. None of these

Q14. The preprocessor directive #include is required if

1. Console output is used

2. Console input is used

3. Both console input and output is used

4. None of these

Q15. The operator << is called

1. an insertion operator

2. put to operator

3. either a or b

4. None of these

Q16. The operator >> is called

1. an extraction operator

2. a get from operator

3. either a or b

4. get to operator

Q17. When a language has the capability to produce new data type, it is called

1. Extensible

Page 18: Ass Introduction to Object Oriented Programming & C++

2. Overloaded

3. Encapsulated

4. Reprehensible

Q18. The C++ symbol <<

1. perform the action of sending the value of expression listed as its right to the outputs

strewn as the left.

2. is used to indicate the action from right to left

3. is adopted to resemble an arrow

4. All the above

Q19. What is a reference?

1. an operator

2. a reference is an alias for an object

3. used to rename an object

4. None of these

Q20. A constructor is called whenever

1. a object is declared

2. an object is used

3. a class is declared

4. a class is used

Q21. State the object oriented languages

1. C++

Page 19: Ass Introduction to Object Oriented Programming & C++

2. Java

3. Eiffel

4. All of the above

Q22. Overload function in C++

1. a group function with the same name

2. all have the same number and type of arguments

3. functions with same name and same number and type of arguments

4. All of the above

Q23. Operator overloading is

1. making c++ operators works with objects

2. giving new meaning to existing c++ operators

3. making new c++ operator

4. both a& b above

Q24. A constructor is called whenever

1. a object is declared

2. an object is used

3. a class is declared

4. a class is used

Q25. A class having no name

Page 20: Ass Introduction to Object Oriented Programming & C++

1. is not allowed

2. can't have a constructor

3. can't have a destructor

4. can't be passed as an argument

Q26. The differences between constructors and destructor are

1. constructors can take arguments but destructor can't

2. constructors can be overloaded but destructors can't be overloaded

3. both a & b

4. None of these

Q27. A destructor takes

1. one argument

2. two arguments

3. three arguments

4. Zero arguments

Q28. Constructors are used to

1. initialize the objects

2. construct the data members

3. both a & b

4. None of these

Q29. In C++ a function contained with in a class is called

Page 21: Ass Introduction to Object Oriented Programming & C++

1. a member function

2. an operator

3. a class function

4. a method

Q30. The fields in a class of a c++ program are by default

1. protected

2. public

3. private

4. None

Q31. A variable is/are

  a.    String that varies during program execution 

b.    A portion of memory to store a determined value

c.    Those numbers that are frequently required in programs

d.    None of these

 

 Q32. Which of the following can not be used as identifiers?

  a.    Letters

b.    Digits

c.    Underscores

d.    Spaces

  

Page 22: Ass Introduction to Object Oriented Programming & C++

Q33. The difference between x and ‘x’ is

  a.    The first one refers to a variable whose identifier is x and the second one

refers to the character constant x

  b.    The first one is a character constant x and second one is the string literal x

  c.    Both are same

  d.    None of above

  

Q34. Which of the following statement is true?

 a.    String Literals can extend to more than a single line of code by putting a

backslash sign at the end of each unfinished line.

b.    You can also concatenate several string constants separating them by one or several

blank spaces, tabulators, newline or any other valid blank character

  c.    If we want the string literal to explicitly made of wide characters, we can

precede the constant with the L prefix

d.    All of above

Q35. Regarding following statement  which of the statements is true?

  const int pathwidth=100;

a.    Declares a variable pathwidth with 100 as its initial value

  b.    Declares a construction pathwidth with 100 as its initial value

  c.    Declares a constant pathwidth whose value will be 100

Page 23: Ass Introduction to Object Oriented Programming & C++

  d.    Constructs an integer type variable with pathwidth as identifier and 100 as

value

  

Q36. If you use same variable for two getline statements

 

a.       Both the inputs are stored in that variable

b.      The second input overwrites the first one

c.       The second input attempt fails since the variable already got its value

d.      You can not use same variable for two getline statements

Q37. The “return 0;” statement in main function indicates

 

a.       The program did nothing; completed 0 tasks

b.      The program worked as expected without any errors during its execution

c.        not to end the program yet.

d.      None of above

Q38. The size of following variable is not 4 bytes in 32 bit systems

 

a.       int

b.      long int

c.       short int

d.      float

Q39. Identify the correct statement regarding scope of variables

 

a.       Global variables are declared in a separate file and accessible from any program.

b.      Local variables are declared inside a function and accessible within the function only.

Page 24: Ass Introduction to Object Oriented Programming & C++

c.       Global variables are declared inside a function and accessible from anywhere in program.

d.      Local variables are declared in the main body of the program and accessible only from

functions.

Q40 cin extraction stops execution as soon as it finds any blank space character

 

a.       true

b.      false