CS201 Viva & Paper Best Preparation
CS201 Viva & Paper Best Preparation
Explanation:
Datatype (Int), Variable (a),
Assignment operator (=), Value (10),
End of statement (;).
4. Function:
Input + Processing + Output
For example (A+B). Input(A), Processing (+),
Output (Result). Mean + is a function
5. Classes:
6. Objects:
Object is a property of class and three type of object.
i) Attribute ii) Behavior iii) Exitance
7. Array:
Collection of Data and Always Start 0.
Declaration with [ ] brackets. Honda
For example, Declaration size of honda [4]:
Output:
Please enter the number (-1 to end input) 1
2
3
4
5
6
-1
The total number of positive integers entered by user is 6
8. Loop:
Three types:
i) While Loop ii) Do While Loop iii) For Loop
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
Output:
0
1
2
3
4
Example
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
Example
for (int i = 0; i < 5; i++) {
cout << i << "\n";
}
Output:
0
1
2
3
4
9. Condition:
Three types:
i) If ii) If else iii) Switch Statement
i) If Code:
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Example
// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}
Output 1:
Output 2:
Enter an integer: -4
You entered a negative integer: -4.
This line is always printed.
10. Pointer:
Pointer is a Reference variable and sign of pointer &
11. Operator:
(+,-,*,/,)
Let's try to understand the concept of call by value in C++ language by the
example given below:
#include <iostream>
using namespace std;
void change(int data);
int main()
{
int data = 3;
change(data);
Note: To understand the call by reference, you must have the basic
knowledge of pointers.
#include<iostream>
using namespace std;
void swap(int *x, int *y)
{
int swap;
swap=*x;
*x=*y;
*y=swap; }
int main() {
int x=500, y=100;
swap(&x, &y); // passing value to function
cout<<"Value of x is: "<<x<<endl;
14. Constructors:
A constructor in C++ is a special method that is automatically called
when an object of a class is created.
To create a constructor, use the same name as the class, followed by
parentheses ():
Declaring destructors
Destructors are functions with the same name as the class but preceded
by a tilde (~).
Several rules govern the declaration of destructors. Destructors:
16. Parameters:
The parameter is referred to as the variables that are defined during a
function declaration or definition. These variables are used to receive
the arguments that are passed during a function call. These parameters
within the function prototype are used during the execution of the
function for which it is defined.
Argument Parameter
When a function is called, the values The values which are defined at the time
that are passed during the call are of the function prototype or definition of
called as arguments. the function are called as parameters.
During the time of call each Parameters are local variables which are
argument is always assigned to the assigned value of the arguments when
parameter in the function definition. the function is called.
// rnum is parameter
18. Aggregation:
In C++, aggregation is a process in which one class defines another
class as any entity reference. It is another way to reuse the class. It is a
form of association that represents HAS-A relationship.
#include <iostream>
using namespace std;
class Address {
public:
string addressLine, city, state;
Address(string addressLine, string city, string state)
{
20. Polymorphism:
A student of C++ needs to know that polymorphism refers to the ability
of a C++ function or object to perform in different ways, depending
on how the function or object is used. Polymorphism is one of the
main features of object-oriented programming.