Professional Documents
Culture Documents
Unit 1
Unit 1
Unit-1
Array
•An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be
accessed randomly using indices of an array.
•They are used to store similar type of elements as in the data type must be the same for all elements.
•They can be used to store collection of primitive data types such as int, float, double, char, etc of any
particular type.
Strings
• Array of Characters
• char a[20]={’a’,’b’,’c’,’d’,.......};
• char name[20]=”pratibha soram”;
• cout<<“pratibha”;
• char surname[20];
• cin>>surname;
• cout<<“soram”;
Sr.No Function & Purpose
1 strcpy(s1, s2);
2 strcat(s1, s2);
3 strlen(s1);
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strrev(s1);
Note: If we do not specify any access modifiers for the members inside the class, then by
default the access modifier for the members will be Private.
Access specifiers (Contd.)
• Public
• All the class members declared under the public specifier will be available to everyone.
• The public members of a class can be accessed from anywhere in the program using the direct
member access operator (.) with the object of that class.
• Private
• The class members declared as private can be accessed only by the member functions inside the
class.
• They are not allowed to be accessed directly by any object or function outside the class.
• Only the member functions or the friend functions are allowed to access the private data members
of the class.
• Protected
• The protected access modifier is similar to the private access modifier in the sense that it can’t be
accessed outside of its class unless with the help of a friend class.
• The difference is that the class members declared as Protected can be accessed by any subclass
(derived class) of that class as well.
• Note: This access through inheritance can alter the access modifier of the elements of base class in
derived class depending on the mode of Inheritance.
Approaches in POP and OOP
POP (Procedural Oriented OOPs (Object Oriented
Programming) Programming)
main(){}
Class1;
subprog1(){} Class2;
Class3;
subprog2(){}
main(){}
C++
- Invented by Bjarne Stroustrup (currently a Professor of Computer
Science at Columbia University)
- Enhanced version of c (c=c+1)
- C was POP
- C++ OOP
C++ (Contd.)
•Header file- iostream
:: scope resolution operator
•class
stud3=stud1+stud2;
Inline Function
• to reduce the function call overhead.
• a function that is expanded in line when it is called.
• when the inline function is called whole code of the inline function gets
inserted or substituted at the point of the inline function call.
• substitution is performed by the C++ compiler at compile time.
• An inline function may increase efficiency if it is small.
• Syntax:
inline return-type function-name(parameters) {
// function code
}
Inline Function (Contd.)
• inlining is only a request to the compiler, #include <iostream>
not a command. using namespace std;
• The compiler can ignore the request for inline int cube(int s) {
inlining for the following circumstances: return s * s * s;
1. If a function contains a loop. (for, }
while and do-while) int main()
2. If a function contains static variables. {
3. If a function is recursive. cout << "The cube of 4 is: " << cube(4);
4. If a function return type is other than return 0;
void, and the return statement doesn’t }
exist in a function body.
5. If a function contains a switch or goto • Output:
statement. The cube of 4 is: 64
Scope resolution operator
1. To access a global variable when there is a local variable with same name
#include<iostream>
using namespace std;
int a; // Global a
int main() {
int a = 23; // Local a
cout << "Value of global x is " << ::x;
cout << "\n Value of local x is " << x;
return 0; Output: Value of global a is 0
} Value of local a is 23
Scope resolution operator (Contd.)
2. To define a function outside a class
#include <iostream> int main()
using namespace std; {
class A { A a;
public: a.fun();
void fun(); // Only declaration return 0;
}; }
void A::fun() { // Definition outside class using ::
cout << "function func() is called"; Output:
} function func() is called
Scope resolution operator (Contd.)
3. To access a class’s static variables. // In C++, static members must be explicitly
defined like this, outside the class
#include<iostream>
int S::x = 1;
using namespace std;
int S::y = 2;
class S{ int main(){
static int x; S s1;
public: int x = 3 ;
static int y; // Local parameter 'x' hides class member 'x', but we can s1.func(x);
access it using ::
cout << "\n S::y = " << S::y;
void func(int x) { return 0;
// We can access class's static variable even if there is a local variable }
cout << "Value of static x is " << S::x; Output:
cout << "\n Value of local x is " << x; Value of static x is 1
} Value of local x is 3
}; S::y = 2
Passing objects as arguments
#include <bits/stdc++.h> cout << "Initial Values \n";
//header file that we use in our code to include all the standard libraries cout << "Value of object 1: " << E1.a
using namespace std; << "\n and object 2: " << E2.a
class Error{ << "\n\n";
public: E2.add(E1); // Passing object as an argument to
function add()
int a;
// Display changed values after passing object as
void add(Error E) { // This function will take an object as an argument argument
a = a + E.a; cout << "New values \n";
} cout << "Value of object 1: " << E1.a
}; << "\n and object 2: " << E2.a
int main(){ << "\n\n";
Error E1, E2; // Create objects return 0;
E1.a = 7; // Values are initialized for both objects }
E2.a = 20;