QUESTIONS AND ANSWERS. 1) To access a global variable when there is a local variable with same name:

Similar documents
Object Oriented Pragramming (22316)

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Unit 1 : Principles of object oriented programming

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Downloaded from

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Darshan Institute of Engineering & Technology for Diploma Studies

PROGRAMMING IN C++ COURSE CONTENT

Chapter 1. Principles of Object Oriented Programming

A SHORT COURSE ON C++

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

Data Structures using OOP C++ Lecture 3

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Scientific Computing

Lab # 02. Basic Elements of C++ _ Part1

Short Notes of CS201

CS2141 Software Development using C/C++ C++ Basics

CS201 - Introduction to Programming Glossary By

Your first C++ program

Introduction to Programming EC-105. Lecture 2

Input And Output of C++

C++ Quick Guide. Advertisements

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Introduction to C++ (Extensions to C)

Introduction to C++ Systems Programming

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

OOP THROUGH C++(R16) int *x; float *f; char *c;

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Fundamentals of Programming CS-110. Lecture 2

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

6.096 Introduction to C++ January (IAP) 2009

UNIT- 3 Introduction to C++

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Creating a C++ Program

2. First Program Stuff

Unit - IV CHAPTER - 13 INTRODUCTION TO OOP WITH C++ Part 1 Choose the best answer

CE221 Programming in C++ Part 1 Introduction

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Introduction to Programming using C++

Computer Programming : C++

- HALF YEARLY EXAM ANSWER KEY DEC-2016 COMPUTER SCIENCE ENGLISH MEDIUM

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Chapter 1: Object-Oriented Programming Using C++

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Lab Instructor : Jean Lai

Introduction to Programming Using Java (98-388)

Fast Introduction to Object Oriented Programming and C++

C++ PROGRAMMING BASICS

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Fundamentals of Programming. Lecture 19 Hamed Rasifard

CS 251 Intermediate Programming Methods and More

CS3157: Advanced Programming. Outline

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Object Oriented Programming

AN OVERVIEW OF C++ 1

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Subject Name: Object Oriented Programming

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Dr. Md. Humayun Kabir CSE Department, BUET

JAVA GUI PROGRAMMING REVISION TOUR III

Bangalore South Campus

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Chapter 15 - C++ As A "Better C"

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Unit-V File operations

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CS242 COMPUTER PROGRAMMING

DE122/DC106 Object Oriented Programming with C++ DEC 2014

Data Structures (INE2011)

CSCE 110 PROGRAMMING FUNDAMENTALS

Sri Vidya College of Engineering & Technology

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Programming. C++ Basics

Functions. Introduction :

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

CS 251 Intermediate Programming Methods and Classes

Programming Language. Functions. Eng. Anis Nazer First Semester

The C++ Language. Arizona State University 1

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

EECS402 Lecture 02. Functions. Function Prototype

Chapter 1 INTRODUCTION

TEMPLATE IN C++ Function Templates

Module Operator Overloading and Type Conversion. Table of Contents

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

2. Functions I: Passing by Value

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI


INTRODUCTION TO PROGRAMMING

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Transcription:

Q1. List the usage of :: operator in C++. [AYUSH BANIK -1741012] QUESTIONS AND ANSWERS 1) To access a global variable when there is a local variable with same name: int x; // Global x int main() int x = 10; // Local x cout << "Value of global x is " << ::x; cout << "\nvalue of local x is " << x; return 0; 2) To define a function outside a class. class A public: // Only declaration void fun(); ; // Definition outside class using :: void A::fun() cout << "fun() called"; int main() A a; a.fun(); return 0;

Q2. Define message passing. [AYUSH BANIK -1741012] Ans) Objects communicate with one another by sending and receiving information to each other. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function and the information to be sent. Following are the basic steps in message passing. Creating classes that define objects and its behaviour. Creating objects from class definitions Q3) List any two limitations of Object Oriented Programming. (AYUSH KUMAR KAYAL,1741013) Ans) Any two limitations of object oriented programming are: 1. Solving a problem using OOP approach consumes more time than the time taken by structured programming approach. 2. Not all programs can be modeled accurately by the objects model. If we just want to read in some data, do something simple to it and write it back out, we have no need to define classes and objects. However, in some OOP languages, we may have to perform this extra step. Q4) Explain Input, Output and Cascading of I/O operators with suitable examples. (AYUSH KUMAR KAYAL,1741013) Ans) INPUT: If the direction of flow of bytes is from device(for example: Keyboard) to the main memory then this process is called input. The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the overloaded operator of extraction(>>) on the cin stream. The operator must be followed by the variable that will store the data. The extraction operator extracts the data from the object cin which is entered using the keyboard. Example: #include<iostream> using namespace std;

int main() int age; cout << "Enter your age:"; cin >> age; cout << "\nyour age is: "<<age; // The input is taken here and stored. return 0; OUTPUT: If the direction of flow of bytes is opposite, i.e. from main memory to device( display screen ) then this process is called output. We know the standard output of a program is the screen, and the C++ stream object defined to access it is cout. cout is used in conjunction with the insertion operator, which is written as <<. #include <iostream> using namespace std; int main( ) int total =98; cout<< Total value: <<total<<endl; // cout is used to access the output return 0; Cascading of I/O operators: The cascading of the input and output operators refers to the consecutive occurrence of input or output operators in a single statement. A program with cascading of the input/output operator #include<iostream> using namespace std; int main () int a, b; cin>>a>>b; Cout<<"The value of a is : "<<a<<"and the value of b is : "<<b<<endl;

return 0; Here, the input is not taken separately for each variable w and similarly multiple output operators can be used in a single line which is known as cascading. It can be observed that cascading of the input/output operator improves the readability and reduces the size of the program. Q5. Define Data Abstraction? [ARPITHA TESS JOSEPH -1741010] Ans) Abstraction is representing essential features without icluding background information or explanation.it abstacts information or attributes.it is done by using data members because they hold information. Q6. Explain the concept of data sharing in user defined member function in C++? [ARPITHA TESS JOSEPH -1741010] Ans) Data sharing in user member function can be implemented by PASSING DATA FROM ONE FUNCTION TI ANOTHER. Exmple: Private: Void input() Int x; Cout<< enter number <<endl; Cin>>x; Fact(x); Void fact(int n) Int f=1,i; For(i=1;i<=n;i++)

F=f*I; Cout<< factorial= <<f; Q7. Develop a function using reference variables (& symbol) as arguments to swap a pair of integers in C++ [ANIKET PAUL-1741007] Ans) void swap(int &a,int &b) int c; c=a; a=b; b=c; cout<<"\ninside function after swapping\nvalue of A is"<<a<<"\nvalue of B is"<<b; Q8. Develop a C++ program to demonstrate enumeration data types with suitable examples. [ANIKET PAUL-1741007 Ans) #include <iostream> using namespace std; enum seasons spring = 34, summer = 4, autumn = 9, winter = 32; int main() seasons s; s = summer; cout << "Summer = " << s << endl; return 0; Output Summer = 4

Q9. List the benefits of object-oriented programming. [AMBER UJJUWAL LINDA-1741006] Ans) ->Benefits of object-oriented programming are: - Objects created for Object Oriented Programs can easily be reused in other programs. Once an Object is created, knowledge of its implementation is not necessary for its use. In older programs, coders needed understand the details of a piece of code before using it. Objects have the ability to hide certain parts of themselves from programmers. This prevents programmers from tampering with values they shouldn't. The object controls how one interacts with the values, preventing other kinds of errors. For example, a programmer (or another program) cannot set the width of a window to - 400. It provides a clear modular structure for programs which makes it good for defining abstract datatypes in which implementation details are hidden. Objects can also be reused within and across applications. It makes software easier to maintain. Since the design is modular, part of the system can be updated. Reuse also enables faster development. Object-oriented programming languages come with rich libraries of objects, and code developed during projects is also reusable in future projects. It provides a good framework for code libraries where the supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces. Better Productivity as OOP techniques enforce rules on a programmer that, in the long run, help her get more work done; finished programs work better, have more features and are easier to read and maintain. Multiple instance of an object can co-exist Polymorphism: the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation. Polymorphism is extensively used in implementing Inheritance. Q10. Explain the unique advantages of using an object-oriented programming principle for develop big projects. [AMBER UJJUWAL LINDA-1741006] Ans) Advantages of using an object-oriented programming to develop big projects are: I. Design Benefits: Large programs are very difficult to write. Object Oriented Programs force designers to go through an extensive planning phase, which makes for better

designs with less flaws. In addition, once a program reaches a certain size, Object Oriented Programs are actually easier to program than non-object-oriented ones. II. III. IV. Software Maintenance: Programs are not disposable. Legacy code must be dealt with on a daily basis, either to be improved upon (for a new version of an exist piece of software) or made to work with newer computers and software. An Object-Oriented Program is much easier to modify and maintain than a non-object-oriented Program. So, although a lot of work is spent before the program is written, less work is needed to maintain it over time. Better Productivity as OOP techniques enforce rules on a programmer that, in the long run, help her get more work done; finished programs work better, have more features and are easier to read and maintain. OOP programmers take new and existing software objects and "stitch" them together to make new programs. Because object libraries contain many useful functions, software developers don't have to reinvent the wheel as often; more of their time goes into making the new program. That s the beauty of encapsulation. Objects are self-contained, and each bit of functionality does its own thing while leaving the other bits alone. Also, this modality allows an IT team to work on multiple objects simultaneously while minimizing the chance that one person might duplicate someone else s functionality. V. Object-oriented programming languages allows you to break down your software into bite-sized problems that you then can solve one object at a time. Q11. List two types of comments in C++? [ABHISHEK RATHI-1741002] Ans)Comments are portions of the code ignored by the compiler which allow the user to make simple notes in the relevant areas of the source code. Comments come either in block form or as single lines. Single-line comments (informally, C++ style), start with // and continue until the end of the line. If the last character in a comment line is a \ the comment will continue in the next line. Multi-line comments (informally, C style), start with /* and end with */.

12) Explain any four differences between Procedure oriented programming and Object-Oriented Programming? [ABHISHEK RATHI-1741002] Ans) BASIS Divided Into PROCEDURE ORIENTED PROGRAMMING In POP, program is divided into small parts called functions. Approach POP follows Top Down approach. Access POP does not have any access Specifiers specifier. OBJECT ORIENTED PROGRAMMING In OOP, program is divided into parts called objects. OOP follows Bottom Up approach. OOP has access specifiers named Public, Private, Protected, etc. Examples Example of POP are : C, VB, FORTRAN, Pascal. Example of OOP are : C++, JAVA, VB.NET, C#.NET. 13)Scope resolution operator (::) in C++ programming language is used to define a function outside a class or when we want to use a global variable but also has a local variable with the same name. [AASTHA SACHDEVA-1741001] Ans) Example : #include <iostream> using namespace std; char c = 'a'; // global variable int main() char c = 'b'; //local variable cout << "Local variable: " << c << "\n"; cout << "Global variable: " << ::c << "\n"; //using scope resolution operator return 0;

OUTPUT: Local variable:b Global variable:a 2. Scope Resolution Operator in class #include <iostream> using namespace std; class programming public: void output(); //function declaration ; // function definition outside the class void programming::output() cout << "Function defined outside the class.\n"; int main() programming x; x.output(); return 0; OUTPUT: Function defined outside the class. 14)List any two Object oriented programming languages other than C++. [ABHISHEK TM -1741003] Ans) The other object oriented programs are java and python. 15)What is a class? Write a program explaining the class declaration and definition. [ABHISHEK TM -1741003] Ans) Class: The building block of C++ that leads to Object Oriented programming is a Class. It is a user defined data type, which holds its own data members and member

functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object. using namespace std; class student public: string studentname; int id; void printname(); void printid() cout << "Geek id is: " << id; ; Void student::printname() cout << "studentname is: " << studentname; int main() student obj1; obj1.studentname = "xyz"; obj1.id=15; obj1.printname(); cout << endl; obj1.printid(); return 0;

Q16. Data Types in c++? [AASTHA SACHDEVA-1741001] Ans) Data types in C++ is mainly divided into two types: 1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char, float, bool etc. Primitive data types available in C++ are: o Integer o Character o Boolean o Floating Point o Double Floating Point o Valueless or Void o Wide Character 2. Abstract or user defined data type: These data types are defined by user itself. Like, defining a class in C++ or a structure. Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -2147483648 to 2147483647. Character: Character data type is used for storing characters. Keyword used for character data type is char. Characters typically requires 1 byte of memory space and ranges from - 128 to 127 or 0 to 255. Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false. Keyword used for boolean data type is bool. Floating Point: Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used for floating point data type is float. Float variables typically requires 4 byte of memory space. Double Floating Point: Double Floating Point data type is used for storing double precision floating point values or decimal values. Keyword used for double floating point data type is double. Double variables typically requires 8 byte of memory space. void: Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which does not returns a value. 3. Datatype Modifiers: As the name implies, datatype modifiers are used with the builtin data types to modify the length of data that a particular data type can hold. Data type modifiers available in C++ are:

Signed Unsigned Short Long Q17. Explain the usage of array of objects in a class with example. [ANIT VIJAY-1741008] Ans) An object of class represents a single record in memory, if we want more than one record of class type, we have to create an array of class or object. As we know, an array is a collection of similar type, therefore an array can be a collection of class type. Class diagram class class-name datatype var1; datatype var2; - - - - - - - - - - datatype varn; ; method1(); method2(); - - - - - - - - - - methodn(); class-name obj[ size ]; PROGRAM: #include<iostream.h> #include<conio.h> class Employee int Id; char Name[25]; int Age; long Salary; public: void GetData() //Statement 1 : Defining GetData()

cout<<"\n\tenter Employee Id : "; cin>>id; cout<<"\n\tenter Employee Name : "; cin>>name; cout<<"\n\tenter Employee Age : "; cin>>age; cout<<"\n\tenter Employee Salary : "; cin>>salary; void PutData() //Statement 2 : Defining PutData() cout<<"\n"<<id<<"\t"<<name<<"\t"<<age<<"\t"<<salary; ; void main() int i; Employee E[3]; //Statement 3 : Creating Array of 3 Employees Output : for(i=0;i<3;i++) cout<<"\nenter details of "<<i+1<<" Employee"; E[i].GetData(); cout<<"\ndetails of Employees"; for(i=0;i<3;i++) E[i].PutData();

Enter details of 1 Employee Enter Employee Id : 101 Enter Employee Name : Suresh Enter Employee Age : 29 Enter Employee Salary : 45000 Enter details of 2 Employee Enter Employee Id : 102 Enter Employee Name : Mukesh Enter Employee Age : 31 Enter Employee Salary : 51000 Enter details of 3 Employee Enter Employee Id : 103 Enter Employee Name : Ramesh Enter Employee Age : 28 Enter Employee Salary : 47000 Details of Employees 101 Suresh 29 45000 102 Mukesh 31 51000 103 Ramesh 28 47000 Q18. Create a class to represent a vector (single dimensional array). Include member functions to perform the following tasks. A) to create the vector B) To modify the value of a given element C) To multiply a scalar value. [ANIT VIJAY-1741008] Ans) #include <iostream.h> #include <conio.h> intconst size=50; class vector float d[size]; int s; public:

void create(void); void modify(void); void multiply(void); void display(void); ; void vector :: create(void) cout<<"\n\nenter of Array you want to create:-"; cin>>s; cout<<"enter "<<s<<" Real Numbers\n"; for(int i=0;i<s;i++) cin>>d[i]; void vector :: modify(void) int mfy_value; float with; cout<<"\nenter Location of array at which value is to be modified:-"; cin>>mfy_value; cout<<"enter Value with which you want to Replace:-"; cin>>with; d[mfy_value]=with; void vector :: multiply(void) int mul; cout<<"\nenter value with which you want to multiply:-"; cin>>mul; for(int i=0;i<s;i++) d[i]=d[i]*mul;

void vector :: display(void) cout<<"\n\ndisplay of Array\n"; cout<<"("; for(int i=0;i<s;i++) cout<<d[i]; if(i!=s-1) cout<<","; cout<<")"; void main() clrscr(); vector o1; int choice; do cout<<"\n\nchoice List\n"; cout<<"1) To Create Vector Array\n"; cout<<"2) To Modify Array\n"; cout<<"3) To Multiply with Scalar value\n"; cout<<"4) To Display\n"; cout<<"5) EXIT\n"; cout<<"enter your choice:-"; cin>>choice; switch(choice) case 1: o1.create(); break; case 2: o1.modify();

break; case 3: o1.multiply(); break; case 4: o1.display(); break; case 5:goto end; while(1); end: Q19. List any six special characteristics of constructors. [ARJUN JOSHUA -1741009] Ans) They should be declared in the public section. They are invoked directly when an object is created. They don t have return type, not even void and hence can t return any values. Constructors can be inside the class definition or outside the class definition. They can t be inherited (although a derived class can call the base class constructor) Like other C++ functions, they can have default arguments. Q20. Explain dynamic initialization of objects using a class. [ARJUN JOSHUA-1741009] Ans) Dynamic initialization of object refers to initializing the objects at run time i.e. the initial value of an object is to be provided during run time. Dynamic initialization can be achieved using constructors and passing parameters values to the constructors. This type of initialization is required to initialize the class variables during run time. #include <iostream> using namespace std; class simple_interest

float principle, time, rate,interest; public: simple_interest (float a, float b, float c) principle = a; time =b; rate = c; void display ( ) interest =(principle* rate* time)/100; cout<<"interest ="<<interest ; ; int main() float p,r,t; cout<<"principle amount, time and rate"<<endl; cout<<"2000 7.5 2"<<endl; simple_interest s1(2000,7.5,2);//dynamic initialization s1.display(); return 1;