Docsity

A complete note on OOP(C++) which is the basic understanding for object oriented concept, Lecture notes of Object Oriented Programming

Partial preview of the text

Download A complete note on OOP(C++) which is the basic understanding for object oriented concept and more Lecture notes Object Oriented Programming in PDF only on Docsity!

A complete note on,

Object oriented

programming

Along with the problems and solutions: also available note on : Data Structure and Algorithms By: Er. Bishwas Pokharel(Lecturer) B.E- Pulchowk campus MSc.Engg. - Pulchowk Campus Published On: 2019/01/

This implies that you cannot declare same variable twice in same function #include using namespace std; int value=10; int main() { int value=100; cout<<value<<endl; cout<<value; return 0; } Output: 100 100 This implies that the value of the global variable is replaced by the value of the local variable. Q. How can you declare and initialize the same variable in one program? #include using namespace std; namespace first { int value=10; } int main() { int value=100; cout<<first::value<<endl; cout<<value<<endl; return 0; } Output: 10 100 namespace allows the use of same variable in two different places but to access the name of variables of namespace you have to use the syntax as name_of_namespace::variable_name (first::value as in above example).

Now Observe, #include using namespace std; namespace first { int value=10; } int main() { cout<<first::value<<endl; return 0; } Output: 10 As you see to access the variable inside you have to use first:: value. Each time :: operator is necessary to access the variable value. But if you use using namespace first; you do not have to write first::value each time. Below example shows a clear clarification. #include using namespace std; namespace first { int value=10; } using namespace first; int main() { cout<<value<<endl; return 0; } Output: Similarly, if you donot write using namespace std; then to access the member cout and cin of the namespace std, each time you have to write the syntax:- name_of_namespace::variable_name (std::cout, std::cin, std::endl etc) as shown in below.. #include namespace first { int value=10; } using namespace first;

return (xx); } } namespace Cube { int num; int func(int x) { return (xx*x); } } using namespace std; int main(void) { cout<<"Enter two different values for num: "<<endl; cin>>Square::num; cin>>Cube::num; cout<<"The square of num used in namespace Cube: "<<Square::func((Cube::num))<<endl; cout<<"The cube of num used in namespace Square: "<<Cube::func((Square::num))<<endl; return 0; } Output: c. int main(void): This specifies the real beginning of our program. Here we are declaring a function named main. Q. Why always main? Is it possible to use any other name besides main? Ans: Compiler has instruction to search for keyword main. If compiler is instructed to search for other name as your choice besides main then the program must have you search name which you instruct to search for compiler. return value 0 is also not necessary; you can return any integer value. But compiler does not care about any value you return. So it is considered better to return 0.

d. cout<<”hello world”<<endl The body of our main function contains only one statement. This statement directs the executing program to print the message hello world on the screen. A statement is the fundamental unit of execution in a C++ program. Functions contain statements that the compiler translates into executable machine language instructions. All statements in C++ end with a semicolon (;). printf() in c but cout in C++ IN C #include<stdio.h> int main(void) { int a; printf("Enter number a: "); scanf("%d",&a); printf("The number is %d\n",a); return 0; } Output: Enter number a: 1 The number is 1

IN C++

#include using namespace std; int main(void) { int a; cout<<"Enter the number a:"; cin>>a; cout<<"The number is: " <<a<<endl; return 0; } Output: Enter number a: 1 The number is 1 Scanf() in c but cin>> in C++ Note: cout<< is used to display on the screen as like printf() where as cin>> is used to input the value. endl is end line it is almost as “\n” in C.

  1. Reference (Be careful it is not pointer’s address as you see as: int *ptr=&a). int a=5; nt &r =a; Here,

Pass by Value #include using namespace std; void swap(int a,int b) { int temp; temp=a; a=b; b=temp; } int main(void) { int x=2,y=3; cout<<"Beforeswap: "<<endl; cout<<x<<" "<<y<<endl; swap(x,y); cout<<"After swap: "<<endl; cout<<x<<" "<<y; return 0; } Pass by Reference #include using namespace std; void swap(int& a,int& b) { int temp; temp=a; a=b; b=temp; } int main(void) { int x=2,y=3; cout<<"Beforeswap: "<<endl; cout<<x<<" "<<y<<endl; swap(x,y); cout<<"After swap: "<<endl; cout<<x<<" "<<y; return 0; } Output: Before swap: 2 3 Before swap: 2 3 After swap: 2 3 After swap: 3 2 In Pass by Value, Analogy example: Suppose ‘A’ copy the text document from Bs laptop and change the text document in As own laptop. The changes occurs in the text document of A’s laptop cannot be visible to B’s document. Similarly, Here value of x is copied in a and value of y is copied in b so change in one variable value does not affect the other. In Pass by Reference, Here, int&a=x anda=2alsoint &b=yand b=3. Afterswapa=3andb=2whichimplies x=3 and y= because both point to the same memory location.

  1. Return by reference In Normal function #include using namespace std; int x; int showx() { return x; } int main() { x=100; int b; b=showx(); cout<<b<<endl; showx()=20; //here it shows error cout<<b; return 0; } In return by reference #include using namespace std; int x; int & showx() { return x; } int main() { x=100; int b; b=showx(); // normal function cout<<b<<endl; showx()=20;// return by ref. cout<x; return 0; } Output: Error- lvalue case(look it) Output: 20 In return by reference, function which return reference variable is an alias for the referred variable as in case of (int & variable_name), in above case it is something like reference variable is created for global variable in statement “showx()=20”; so the value of the global variable change from 100 to 20. Problem 2: Write a function that passes two temperatures by reference and sets the larger of the two numbers to 100 by using return by reference. Solution: #include using namespace std; int &temp(int &a,int &b) { if(a>b) { return a; } else { return b; }

#include using namespace std; void m1(char c) { cout<<"char-args:"<<endl; } void m1(int i) { cout<<"int- args:"<<endl; } void m1(int i,float f) { cout<<"int- float-args:"<<endl; } void m1(float f,int i) { cout<<"float-int-args:"<<endl; } int main() { m1('c'); m1(2); m1(3,2.0); m1(2.0,3); return 0; } Output: char-args: int-args: int- float-args: float-int-args But when you run the below program you may find an unexpected result due to the promotion rules. #include using namespace std; void m1(int i)

cout<<"int- args:"<<endl; } void m1(int i,float f) { cout<<"int- float-args:"<<endl; } int main() { m1('c'); m1(3,2.0); m1(2.0,3); return 0; } Output: Here, when function is called with the character argument (as m1(‘c’)) it does not find the match so according to the rule “char” is promoted to “int”. Here it finds the match (m1(int )) and statement of it gets executed through m1(‘c).

  1. Conversion rules #include using namespace std; int main() { int a=2,b=3; float c; c=a/b; cout<<"The value of c:"<<c<<endl; return 0; } Output The value of c: 0 Here, the output must be 0.666667 but it shows 0. Why? Ans: (2/3)=(0.66667) but as default it takes only integer value and so 0 becomes output.

space. If function is written at the place from where it’s called then total time taken = T (fe). If again same function is necessary somewhere in the program then again 64 KB memory is consumed by it if function is written without calling it. But function call saves next 64 KB of memory space. Here in such scenario, time taken to execute function increases but it increases memory efficiency i.e. memory space consumed by it is reduced. But this scenario is feasible only if the function statement in program is very long. But if program is short then it consumes small fraction of memory space which is not considered big problem, If program is written every time when necessary. Here time is in big important so programmer used inline keyword to reduce time taken to execute function by concept as: when function in necessary in program just copy function definition in place from where it’s called rather than to transfer control. Inline is just a request to the compiler, and compiler determines whether to accept the request or to reject the request. 9.Function Overloading Function overloading occurs in the program when number of function has the same name but different type of arguments. Advantages:

  • Having same name for all function reduces ambiguity because it is easy to remember the name of function. #include using namespace std; void m1(int i) { cout<<"Int-args:"<<endl; } void m1(float f) { cout<<"Float-args:"<<endl; } int main() { m1(2); m1(2.3f); return 0; }

Output: Int-args: Float-args: Here, both functions have same name but different arguments. When integer is passed then “Int- args:” is executed and when float is passed then “Float-args” is executed. Problem 3. Write a program using the function overloading that converts feet to inches. Use function with no argument, one argument and two arguments. Decide yourself the types of arguments. Use pass by reference in any one of the Function above. Solution: #include using namespace std; void feetToinch() { cout<<"Enter the feet_value: "; float feet_value; cin>>feet_value; float inch_value= feet_value12; cout<<"corresponding inch_value: "<<inch_value<<endl; } void feetToinch(float &inch_value) { inch_value= inch_value12; } void feetToinch(float feet_value, float conversion_value) { float inch_value= feet_value*conversion_value; cout<<"corresponding inch_value: "<<inch_value<<endl; } int main() { cout<<"Enter 0 for no argument, 1 for one argument and 2 for two argument: "<<endl; int choice_num; cin>>choice_num; if(choice_num==0) { feetToinch(); }else if(choice_num==1) { cout<<"Enter the feet_value: "; float feet_value; cin>>feet_value; feetToinch(feet_value);

Output: In above program, if user do not pass any information about the base value then by default it assumes that the base is decimal as (int base=10) else it is replaced by the argument. When print(50,8) is passed in the function then base is taken as 8 in place of 10. So it gives the octal value of the 50 as 62. Note: default value must be assigned from the right side. Ex: void sum(int a=2,int b) // error void sum(int a=2,int b,int c=12) // error void add(int a, int b=1, int d=11) //valid Example: #include using namespace std; void add(int a, int b=10) { int c=a+b; cout<<"value of c is:"<<c<<endl; } int main() { int a=2,b=3; add(a); add(a,b); return 0; } Output: Here, add (a) has only one value passed in function. Here argument int b =10 tells the compiler that if no any value is passed in me than I considered default value 10 as my argument otherwise I take the value which is passed for me. It is the concept of default argument.

  1. Features of C++ a. Namespace: It provides an encapsulation i.e member of namespace can be used without knowing any necessary details about the member. b. Function Overloading: It improves the readability of code. c. Inline function: It speeds up execution time for small code. d. Reference variable: It works for the memory efficiency, no need to create new variable and copy value to it. e. Default argument: It provides flexibility. f. Generics: It provides reusability and more powerful features through template. g. Object oriented: It prefers creation of objects for large program to handle data with high security.
  2. C++ vs C a. More Libraries in C++ than C b. In C++, less reliance on preprocessor i.e #define is replaced by const c. More type safe features than C d. In C++, declaration can be done anywhere but the freedom is restricted in C. e. C uses pointer to swap and focused in pointer especially where as C++ prefer concept of reference variable. f. C++ focus on Object oriented where as C is imperative.