Anzeige
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Anzeige
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Anzeige
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Bcsl 031 solve assignment
Nächste SlideShare
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Wird geladen in ... 3
1 von 13
Anzeige

Más contenido relacionado

Anzeige

Más de Indira Gnadhi National Open University (IGNOU)(19)

Anzeige

Bcsl 031 solve assignment

  1. Question 1: (a) Explain why Object Oriented Programming approach is better than Structured Programming Approach. (5 Marks) Ans :- For the most part, web developers the world over have to contend with programming their back-end applications using either structured or object-oriented programming. So what exactly do those terms mean? Keep reading to find out as I compare structured and object oriented programming in this article. • Definition of Structured Programming Structured programming can be defined as a Software application programming technique that follows a top down design approach with block oriented structures. This style of programming is characterized by the programmers tendency to divide his program source code into logically structured blocks which would normally consist of conditional statements, loops and logic blocks. This style of programming has the implementation of the source code being processed in the order in which bits of the code have been typed in. • Definition of Object-Oriented Programming Object-oriented programming can be defined in simplest terms as software application programming where there is an interaction between self-contained miniprograms or objects within the main program. In other terms, object-oriented programming can be known as the process of using several classes to represent different areas of functionality or data objects within your software application. These data objects have data fields and functions that act on the data fields. The hold three main characteristics which are encapsulation, inheritance, and polymorphism. Examples of objects would include windows, menus, text inputs, icons, etc. There must be procedures to manipulate them.
  2. (b) Write a simple C++ program to explain the basic structure of C++ programming. (5 Marks) Ans :- Probably the best way to start learning a programming language is by writing a program. Therefore, here is our first program: 1 2 3 4 5 6 7 8 9 10 // my first program in C++ #include <iostream> using namespace std; int main () { cout << "Hello World!"; return 0; } Hello World! The first panel (in light blue) shows the source code for our first program. The second one (in light gray) shows the result of the program once compiled and executed. To the left, the grey numbers represent the line numbers - these are not part of the program, and are shown here merely for informational purposes. The way to edit and compile a program depends on the compiler you are using. Depending on whether it has a Development Interface or not and on its version. Consult the compilers section and the manual or help included with your compiler if you have doubts on how to compile a C++ console program. The previous program is the typical program that programmer apprentices write for the first time, and its result is the printing on screen of the "Hello World!" sentence. It is one of the simplest programs that can be written in C++, but it already contains the fundamental components that every C++ program has. We are going to look line by line at the code we have just written: // my first program in C++ This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is. #include <iostream> Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program. using namespace std; All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.
  3. int main () This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function. The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them. Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed. cout << "Hello World!"; This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program. cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen). cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code. Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement). return 0; The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program. (c) Explain the usage of the following C++ operators with the help of an example program. (6 Marks) A) sizeof operator :- The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.
  4. The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type. The syntax of using sizeof is as follows: sizeof (data type) Where data type is the desired data type including classes, structures, unions and any other user defined data type. B ) Logical Operators :- An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides the following types of operators: • • • • • • Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators This chapter will examine the arithmetic, relational, logical, bitwise, assignment and other operators one by one. Arithmetic Operators: There are following arithmetic operators supported by C++ language: Assume variable A holds 10 and variable B holds 20, then: Show Examples Operator Description + Adds two operands Subtracts second operand from the first * Multiplies both operands / Divides numerator by de-numerator Modulus Operator and remainder of after % an integer division Increment operator, increases integer value ++ by one -Decrement operator, decreases integer Example A + B will give 30 A - B will give -10 A * B will give 200 B / A will give 2 B % A will give 0 A++ will give 11 A-- will give 9
  5. value by one C ) Scope resolution operator :- The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example: int count = 0; int main(void) { int count = 0; ::count = 1; // set global count to 1 count = 2; // set local count to 2 return 0; } The declaration of count declared in the main() function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope. You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator. In the following example, the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator. #include <iostream> using namespace std; class X { public: static int count; }; int X::count = 10; int main () { int X = 0; cout << X::count << endl; } Question 2: // define static data member // hides class type X // use static member of class X
  6. (a) Define the class Book with all the basic attributes such as title, author, publisher, price etc. Define the default constructor, member functions display_data() for displaying the Book details. Use appropriate access control specifiers in this program. (8 Marks) Ans :- class Book{ public: enum Genre{ fiction = 0, nonfiction, periodical, biography, children }; class Invalid{ //... }; Book(int isbn, string author, string title, string copyright, Genre genre, string status) Book(); int isbn(); const{return isbn;} string author(); const{return author;} string title(); const{return title;} string copyright(); const{return copyright;} Genre genre(); const{return genre;} string status(); const{return status;} private: int isbn; string author; string title; string copyright; Genre genre; string status; }; (b) Explain the following terms in the context of object oriented programming. Also explain how these concepts are implemented in C++ by giving an example program for each. (a) (b) (8 Marks) Abstraction :- Data abstraction refers to, providing only essential information to the outside word and hiding their background details, i.e., to represent the needed information in program without presenting the details. Encapsulation :- Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions existing inside the class. Data encapsulation led to the important concept of data hiding. Data hiding is the implementation details of a class that are hidden from the user. The concept of restricted access led programmers to write specialized functions or methods for performing the operations on hidden members of the class. Attention must be paid to ensure that the class is designed properly.
  7. (c) Operator Overloading :- using the assignment operator is fairly straightforward, correctly implementing an overloaded assignment operator can be a little more tricky than you might anticipate. There are two primary reasons for this. First, there are some cases where the assignment operator isn’t called when you might expect it to be. Second, there are some issues in dealing with dynamically allocated memory (which we will cover in the next lesson). Static Member :- C++ introduces two new uses for the static keyword when applied to classes: static member variables, and static member classes. Before we go into the static keyword as applied to member variables, first consider the following class: 1 2 class Something 3 { 4 private: int m_nValue; 5 public: Something() { m_nValue = 0; } 6 }; 7 8 9 int main() 10{ Something cFirst; 11 Something cSecond; 12 return 0; 13} 14 When we instantiate a class object, each object gets it’s own copy of all normal member variables. In this case, because we have declared two Something class objects, we end up with two copies of m_nValue — one inside cFirst, and one inside cSecond. cFirst->m_nValue is different than cSecond->m_nValue. Question 3: (a) What is polymorphism? What are different forms of polymorphism? Explain implementation of polymorphism with the help of a C++ program. (8 Marks) Ans :- The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.
  8. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. #include <iostream> using namespace std; class Shape { protected: int width, height; public: Shape( int a=0, int b=0) { width = a; height = b; } int area() { cout << "Parent class area :" <<endl; return 0; } }; class Rectangle: public Shape{ public: Rectangle( int a=0, int b=0) { Shape(a, b); } int area () { cout << "Rectangle class area :" <<endl; return (width * height); } }; class Triangle: public Shape{ public: Triangle( int a=0, int b=0) { Shape(a, b); } int area () { cout << "Rectangle class area :" <<endl; return (width * height / 2); } }; // Main function for the program int main( ) { Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); // store the address of Rectangle shape = &rec; // call rectangle area. shape->area();
  9. // store the address of Triangle shape = &tri; // call triangle area. shape>area(); return 0; } (b) What is friend function? How it is implemented in C++? Explain advantages of using friend function with the help of an example. ( 8 Marks) Ans :- A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows: class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); }; Question 4 : (a) Explain the following functions for manipulating file pointers, with the help of example program: (8 Marks) • seekg() :- Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it calls either pubseekpos (1) or pubseekoff (2) on its associated stream buffer object (if any). Finally, it destroys the sentry object before returning. • seekp() :- This function simply copies a block of data, without checking its contents nor appending a null character at the end. If the input sequence runs out of characters to extract (i.e., the end-of-file is reached) before n characters have been successfully read, the array pointed by s contains all the
  10. characters read until that point, and both the eofbit and failbit flags are set for the stream. • • (b) tellg() :- the function accesses the input sequence by first constructing a sentry object (with noskipws set to true) without evaluating it. Then, if member fail returns true, the function returns -1. Otherwise, returns rdbuf()->pubseekoff(0,cur,in). Finally, it destroys the sentry object before returning. tellp() :- Returns the position of the current character in the output stream. What is an exception? How an exception is different from an error? Explain how exceptions are handled in C++, with the help of an example program. (8 Marks) Ans :- Exception handling is a mechanism that separates code that detects and handles exceptional circumstances from the rest of your program. Note that an exceptional circumstance is not necessarily an error. When a function detects an exceptional situation, you represent this with an object. This object is called an exception object. In order to deal with the exceptional situation you throw the exception. This passes control, as well as the exception, to a designated block of code in a direct or indirect caller of the function that threw the exception. This block of code is called a handler. In a handler, you specify the types of exceptions that it may process. The C++ run time, together with the generated code, will pass control to the first appropriate handler that is able to process the exception thrown. When this happens, an exception is caught. A handler may rethrow an exception so it can be caught by another handler. Question 5: (a ) What is template? Write appropriate statements to create a template class for Stack data structure in C++. (8 Marks) Ans :- Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type. A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept. There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>. #include <iostream> #include <vector>
  11. #include <cstdlib> #include <string> #include <stdexcept> using namespace std; template <class T> class Stack { private: vector<T> elems; // elements public: void push(T const&); // push element void pop(); // pop element T top() const; // return top element bool empty() const{ // return true if empty. return elems.empty(); } }; template <class T> void Stack<T>::push (T const& elem) { // append copy of passed element elems.push_back(elem); } template <class T> void Stack<T>::pop () { if (elems.empty()) { throw out_of_range("Stack<>::pop(): empty stack"); } // remove last element elems.pop_back(); } template <class T> T Stack<T>::top () const { if (elems.empty()) { throw out_of_range("Stack<>::top(): empty stack"); } // return copy of last element return elems.back(); } int main() { try { Stack<int> intStack; Stack<string> stringStack; // manipulate int stack intStack.push(7); cout << intStack.top() <<endl; // stack of ints // stack of strings
  12. // manipulate string stack stringStack.push("hello"); cout << stringStack.top() << std::endl; stringStack.pop(); stringStack.pop(); } catch (exception const& ex) { cerr << "Exception: " << ex.what() <<endl; return -1; } } (b) What is inheritance? What are different types of inheritance? Explain advantages of using inheritance. (8 Marks) Ans :- One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on. A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form: #include <iostream> using namespace std; // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; }
  13. protected: int width; int height; }; // Derived class class Rectangle: public Shape { public: int getArea() { return (width * height); } }; int main(void) { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; return 0; } For More Ignou Solved Assignments Please Visit - www.ignousolvedassignments.com Connect on Facebook : http://www.facebook.com/pages/IgnouSolvedAssignmentscom/346544145433550 Subscribe and Get Solved Assignments Direct to your Inbox : http://feedburner.google.com/fb/a/mailverify?uri=ignousolvedassignments_com
Anzeige