C++ Programming Language
1. #include <iostream>
using namespace std;
int main()
{
cout<<"this is statement 1\n";
cout<<"this is statement 2\n";
cout<<"this is statement 3\n";
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
2. #include <iostream>
using namespace std;
int main()
{
int a;
float b;
double c;
char d;
string e;
cout<<"Enter your integer value";
cin>>a;
cout<<"Enter your float value";
cin>>b;
cout<<"Enter your double value";
cin>>c;
cout<<"Enter your character value";
cin>>d;
cout<"Enter your string value";
cin>>e;
cout<<"integer value is:"<<a>>"\n";
cout<<"float value is:"<<b>>"\n";
cout<<"double value is:<<c>>"\n";
cout<<"character value is:<<d>>"\n";
cout<<"string value is:<<e>>"\n";
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
3. write a program to read integer value from user and print the given value is +ve or -ve or zero
#include <iostream>
using namespace std;
int main ( )
{
int a;
cout<<"Enter your number";
cin>>a;
if (a>0)
{
cout<<a<<"is +ve number";
}
else if (a<0)
{
cout<<a<<"is -ve number";
}
else
{
cout<<a<< is zero";
}
return 0;
};
-----------------------------------------------------------------------------------------------------------------------------
4. Find Even number or Odd numbers
#include <iostream>
using namespace std;
int main( )
{
int a;
cout<<""Enter your number";
cin>>a;
if (a%2==0)
{
cout<<a<<"is Even number";
}
else
{
cout<<a<<"is Odd number";
}
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
5. Print 1 to 10 Numbers
#include<iostream>
using namespace std;
int main()
int i;
for (i=1;i<=10;i++)
{
cout<<i<<"\n";
}
-----------------------------------------------------------------------------------------------------------------------------
6. Print 1 to 10 Even numbers
#include <iostream>
using namespace std;
int main ( )
{
int i;
for (i=1;1<=10;i++)
{
if (i%2==0)
{
cout<<i<<"\n";
}
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
7. Sum of 1 to 10 Numbers
#include<iostream>
using namespace std;
int main ( )
int = i, sun = 0;
for (i=1;1<=10;i++)
{
sum=sum +i;
}
cout<<"sum is"<<sum;
{
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
8. Print 1 to 50 Odd Numbers
#include<iostream>
using namespace std;
int main ( )
{
int i, sum=0;
for (i=1; i<=50; i++)
{
if (i%2!=0)
{
sum=sum+i;
}
}
cout<<"sum is"<<sum;
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
9. Write a program to find 1 to 500 perfect numbers
#include<iostream>
using namespace std;
int main ( )
{
cout<<"\n\n Find the perfect numbers between 1 and 500 : \n";
cout<<".....................\n";
in i=1, u=1, sum=0;
cout<<"\n The perfect numbers between 1 and 500 are : \n"
while (i<=500)
{
while (u<=500)
{
if (u<i)
{
if (i%4==0)
sum=sum+4;
}
4++;
}
if (sum==i)
}
cout<<i<<" "<<"\n";
}
i++;
4=1;
sum=0;
}
}
-----------------------------------------------------------------------------------------------------------------------------
Example:-
class vehicle
{
int wheel = 4
int engine = 1
int steering = 1
steering colour = "red"
void movie ( )
void sound ( )
void break ( )
}
Object : Object is a real entity
Example:-
Pen, Car, Mobile, Marker, House, Tree etc...,
Each object contains two properties,
they are:-
1. State
2. Behaviour
Car Object:-
State:-
1. Colour
2. Shape
3. Engine
4. Wheels
Etc....
Behavior:-
1. Movie ( )
2. Speed ()
3. Horn ()
4. Break ()
Etc.....
⇒ What is Class:-
* Class is a collection of objects.
(OR)
* Class is a blue print of an object.
(OR)
* Class is a collection of states and behaviour.
(OR)
* Class is a collection of data members and member method.
⇒ Class Syntax:-
Class Class_name
{
state
+
behavior
}
Example:-
class vehicle
{
int wheel = 4;
int engine = 1;
int steering = 1;
steering colour = "red";
void movie ( )
void sound ( )
void break ( )
}
⇒ Principles of OOPs:-
* Encapsulation
* Data Abstraction
* Inheritance
* Polymorphism
⇒ What is Data Hiding?
* By taking private variables we can achieve data hiding.
Class Bank
{
private double balance = 90,000.67;
public double get balance ( )
{
// validations
return balance;
}
}
⇒ Advantages of Data Hiding?
* Security.
⇒ Data Abstraction is nothing but highlighting the required information by using GUI we can active the data.
⇒ Advantages of Data Abstraction:-
1. Security
2. Efficiency
3. Maintainability.
Definitions:-
⇒ Inheritance:- Inheritance is a process of aqua-ring the properties from one class to another class.
⇒ Encapsulation:- Raping up of data members and members method is called Encapsulation.
Ex:- Class
⇒ Polymorphism:- Polymorphism means ability to form more than one form.
Sorting of the given array:-
* Write a program to sort a given array#include<iostream>
using namespace std;
void main()
{
int a [20]
int i, n, j, temp;
cout<<"Enter the value of n : "<<end1;
cin>>n;
cout<<"\n Enter the value : \n;
for (i=0; i<n; i++)
{
cin>>a [i];
{
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (a [j]>a [j+1])
{
temp = a [j];
a [j] = a [j+1];
a [j+1] = temp;
}
}
}
cout<<"sorted array :\n";
for (i=o; i<n; i++)
{
cout<<a [i]<<"\n";
}
}
}
};
-----------------------------------------------------------------------------------------------------------------------------
⇒ Class Syntax Example:-
#include<iostream>
using namespace std;
class example
{
private :
int a = 90;
public :
void set details ( )
{
cout<<a;
}
}
int main ( )
{
Example e ;
e.set details ( );
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
* Addition and Subtraction from the user given value
#include<iostream>
using namespace st;
class example
{
private:
int a, b;
public:
void add( )
{
cout<<"Enter Your 'a' Value";
cin>>a;
cout<<"Enter Your 'b' Value";
cin>>b;
cout<<"addition is "<<a+b<<"\n";
}
void sub( )
{
cout<<"Enter Your 'a' Value";
cin>>a;
cout<<"Enter Your 'b' Value";
cin>>b;
cout<<"subtraction is "<<a-b<<"\n";
}
};
int main( )
{
example e;
e. add( );
e. sub( );
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
* Write a program to sort a given array and show 5 numbers
#include<iostream>
using namespace std;
class example
{
int arr[ 5 ], temp = 0;
public:
void setdetails ()
{
cout<<"Enter your value";
for (int i=0; i<5, i++)
{
cin>>arr[ i ];
}
void increasing or decreasing ( )
{
for (int i=0; i<5; i++)
{
for (int j=i+1; j<5; j++)
{
if (arr [ i ]>arr [ j ])
{
temp = arr [ i ];
arr[ i ] = arr [ j ];
arr[ j ] = temp;
}
}
}
}
void display( )
{
for (int i=0; i<5; i++)
{
cout<<arr[ i ] <<"\t";
}
};
int main( )
{
example e;
e.set details( );
e.increasing order( );
e.display( );
return 0;
}
}
};
-----------------------------------------------------------------------------------------------------------------------------
⇒ Inheritance:-
Example:-
⇒ Single Level Inheritance:-
#include<iostream>
using namespace std;
class A
{
public:
void sub( )
{
cout<<"A class sub method";
}
};
class B: public A
{
public:
void add( )
{
cout<<"B class add method";
}
};
int main( )
{
B obj;
obj.add( );
obj. sub( );
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
⇒ Method Overloading
If two or more method having same name and different arguments, then we can say (Those methods are overloaded method)
class A
{
public:
void add (int a)
{
}
void add (int a, int b)
{
}
}
-----------------------------------------------------------------------------------------------------------------------------
Overloading is posible in 3 ways
1. Order of arguments different.
class A
{
public:
void add (int a, float b)
{
}
void add (float a, int b)
{
}
2. Number of parameters different.
class A
{
public:
void add (int a)
{
}
void add (int a, int b)
{
}
3. Typr of argumnts different.
class A
{
public:
void add (int a, int b)
{
}
void add (float a, float b)
{
}
-----------------------------------------------------------------------------------------------------------------------------
⇒ Method Over-riding
If more than one method having same name and same parameters in two different classes (super and sub classes). Then we can say those methods are over-riden method.
class A
{
public:
void add(int a)
{
}
};
class B: public A
{
public:
void add (int a)
{
}
};
-----------------------------------------------------------------------------------------------------------------------------
⇒ Single Inhertance
#unclude<iostream>
using namespace std;
class A
{
public:
int a=90;
void add( )
{
cout<<"addition"<<"\n";
}
};
class B: public A
{
public:
void add( )
{
cout<<"hai";
}
};
int main( )
{
b.obj;
obj.add( );
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
⇒ Overloading
#include<iostream>
using name space std;
class A
{
public
void add(int a)
{
cout<<a<<"\n";
}
void add (int a, float b)
{
cout<<a<<"-"<<b<<"n";
}
void add (float a, int b)
{
cout<<a<<"-"<<b<<"\n";
}
};
int main( )
{
A obj;
obj.add(3,489.7);
obj.add (45);
obj.add(23.4,45);
}
-----------------------------------------------------------------------------------------------------------------------------
⇒ Overloading
#include<iostream>
using namespace std;
class A
{
public:
void add(int a)
{
cout<<"super class add method"<<"\n";
}
void sub (int a)
{
cout<<"super class sub method"<<"\n";
}
};
class B: public A
public:
void add (int a)
{
cout<<"sub class add mehod";
}
};
int main( )
{
B obj;
obj.add (20);
obj.sub(23);
}
-----------------------------------------------------------------------------------------------------------------------------
Super class object
we can't pass to subject class reference variable. But, we can pass sub class object to super class
⇒ Virtual Method
#include<iostream>
using namespace std;
class A
{
public:
void add (int a)
{
cout <<"super class add method"<<"\n";
}
void sub (int a)
{
cout <<"super class add method"<<"\n";
}
virtual void add 1 ( )
{
cout<<"virtual add method of a class";
}
};
class B. public A
{
public:
void add (int a)
{
cout<<"sub class add method";
}
void add 1 ( )
{
cout<<"add 1 method of B class";
}
};
int main ( )
{
B b;
A * a;
a = & b;
a → add 1 ( );
}
Note:-
→ While calling the super class virtual method, compiler will give chance for sub class over-riding method. If such class does not contain over-ridden method, then the super class method is exceeded.
-----------------------------------------------------------------------------------------------------------------------------
Interface:-
→ interface is collection of pure virtual methods.
Class A
{
Virtual void add( )=0;
virtual void sub( )=0;
}
#include<iostream>
using namspace std;
class A
{
Virtual void add( )=0;
virtual void sub( )=0;
};
class B: public A
{
public:
void add( )
{
cout<<"B class add method";
}
void sub( )
{
cout<<"B class sub method";
}
};
int main( )
{
B obj;
obj.add( );
obj.sub( );
}
-----------------------------------------------------------------------------------------------------------------------------
Abstract Class:-
→ It is a collection of 0 - 100% pure virtual methods.
class A
{
virtual void add 1 ( )=0;
virtual void add 2 ( )=0;
virtual void add 3 ( )=0;
}
class A
{
virtual void add 1 ( )=0;
public:
void add 2 ( )
{
};
virtual void add 3 ( )=0;
}
-----------------------------------------------------------------------------------------------------------------------------
HAS - A Releationship:-
#include<iostream>
#include"A.cpp"
using namespace std;
class b
{
public:
void sub( )
{
cout<<"b class sub method";
}
};
int main( )
{
b obj;
A a;
a.add( );
obj.sub( );
}
-----------------------------------------------------------------------------------------------------------------------------
OOPs:-
Encapsulation:- Encapsulation is a process of wrapping up of data members (variable) and member methods (Methods).
By using class we can achieve encapsulation
Example:-
class A
{
public
int a=90;
void add ( )
{
}
}
In the above example we are wrapping or binding variable "a" and method in a block.
✱ Access specifers in CPP:-
1. Private:- Private is the default access specifier in cpp. Private content is belongs to only class.
class A
{
private:
int a = 90;
}
class B: public A
{
public:
void add( )
{
cout<<a; → This line is error line, Because private variable a is belongs to A class only.we can't use out side of class.
}
};
2. Public:- Public content we can access in any class.
3. Protected:- Protected content we can access with in class and it's sub class only.
Abstraction:- Highlighting the required information by using interface and abstract classes, we can achieve abstraction.
→ Interface:- Interface is the collection of 100% pure virtual methods.
→ Abstract class:- Abstract class is the collection of 0 to100% pure methods.
Interface:-
#include<iostream>
using namespace std;
class bank
{
virtual void add 1 ( ) = 0;
virtual void add 2 ( ) = 0;
virtual void add 3 ( ) = 0;
};
class Shonu: public bank
{
public:
void add 1 ( )
{
cout<<"add 1 metho is done";
}
void add 2 ( )
{
cout<<"add 2 method is don";
}
void add 3 ( )
{
cout<<"add 3 method is done";
}
};
int main ( )
{
Shonu obj;
obj.add 1 ( );
obj.add 2 ( );
obj.add 3 ( );
}
Next Method:-
#include<iostream>
using namespace std;
class Shonu
{
public:
void add 1 ( )
{
cout<<"Shonu add 1 method is done \n";
}
void add 2 ( )
{
cout<<"Shonu add 2 method is done";
}
};
class Amit : public Shonu
{
public :
void add 1 ( )
{
cout<<"Amit add 1 mthod is done \n";
}
void add 2 ( )
{
cout<<"Amit add 2 mthod is done \n";
}
};
int main ( )
{
Amit obj;
Shonu * m;
m = &obj;
m → add 1 ( );
m → add 2 ( );
}
-----------------------------------------------------------------------------------------------------------------------------
1. Error:- Any syntactical mistake is called error
Example:-
2. Exception:- Exception is a unexcepted event or logical error.
⇉ The problem with exception is abnormal termination of a program.
⇉ To handle exception we have three keywords
1. Try
2. Catch.
3. Throw
Handling the exception:-
#include<iostream>
using namespace std;
class A
{
public:
void add ( )
{
int a = 10, b = 2;
cout<<"statement 1\n";
cout<<"statement 2\n";
cout<<"statement 3\n";
try {
if (b = = 0)
throw'1';
cout<<a/b<<"\n";
}
catch (char s)
{
cout<<"we shoud not enter any number/zero";
cout<<"after exception";
cout<<"end";
}
};
int main ( )
{
b obj;
obj.add (1);
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------
Constructor:- It is a special method which is used to allocate memory for class.
Rules to declare constructor:-
1. Constructor do not allow return type.
2. Constructor name should be class name only.
⇉ Can we overload constructor or not?
Answer:- Yes
#include<iostream>
using namespace std;
class A
{
public:
A ( )
{
cout<<"constructor";
}
A ( int a)
{
cout<<"parameterized contructor";
}
};
Destructors:- Destructors are used to the allocate memory of class. destructors will be excuted after compleation of all method in class.
What is ( : : ):-
# include <iostream>
using namespace std;
class A
{
public:
void sub ( );
void add ( );
{
cout<<"add method";
}
};
void A : : sub ( )
{
cout<<"sub method of class A";
}
int main ( )
{
A obj;
obj. sub ( );
obj. add ( );
return 0;
};
No comments:
Post a Comment