C Plus Plus
C Plus Plus
History of c++:
#include <iostream>
Include mean add kro.
Iostream.
.Input output stream
Using namespace.
Its use the name of file for exude the program.
Cout mean console output
Cin.. mean console input
Main is a function which is entry point of the program or stomach of the program.
Which gives data to the io stream etc.
Semi colon error btaata he.
Int 0 == 0 to 999
#include<iostream>
using namespace std;
main()
{
int x = 20;
int y = 30;
if ( x==20)
{
cout << "Naveed ";
}else
{
cout << "pakistan ";
}
}
#include<iostream>
using namespace std;
int main(){
int y;
y = 1;
do
{
cout<<"value is of loop is" <<y<<endl;
y = y+1;
}
while(y < 10);
}
#include<iostream>
using namespace std;
int main(){
int num1;
int num2;
char x;
while(true){
cout<<"Enter the 1st value for arithmetition"<<endl;
cin>>num1;
cout<<"Enter the 2nd value for arithmetition"<<endl;
cin>>num2;
cout<<"Enter Minus(-)for substraction"<<endl;
cout<<"Enter Plus(+)for Addition"<<endl;
cout<<"Enter Multiple(*)for Multiplication"<<endl;
cout<<"Enter divide(/)for divion"<<endl;
cout<<"Enter Now"<<endl;
cin>>x;
}
else if(x=='+')
{
}
else if(x=='/')
{
cout<<" You have entered Plus(/) for divion "<<endl;
cout<<" Your entered value is("<<num1<<"/"<<num2<<")"<<endl;
cout<<" Result of Divide value is"<<endl;
cout<<num1 / num2<<endl;
}
else
{
cout<<"Invalid input"<<endl;
}
}
return 0;
#include<iostream>
using namespace std;
class sara{
//privang name = "sara1";// ye mistake theee
//sting and pvt check krne apni
private:
string name = "sara1";
string address = "rawalpindi";
int age = 15;
public:
string talk = "talks too much";
void setName(string n){
name = n;
}
void setAddress(string a){
address = a;
}
void setAge(int a){
age = a;
}
string getName(){
return name;
}
string getAddress(){
return address;
}
int getAge(){
return age;
}
};
int main(){
sara A1;
//cout<<A1.getName();
//cout<<A1.getAddress();
//cout<<A1.talk;
//cout<<A1.getAge();
cout<<A1.getName()<<endl;
cout<<A1.getAddress()<<endl;
cout<<A1.talk<<endl;
cout<<A1.getAge()<<endl;
}
Function
Rules function
1.Agr ksi cheez ka tarteeb se increase ya decrease hona there will use function.
2.Calculater. Main be function use hoga.
3. argument ko call krwaney ke liye.
4.error door krney ke liye.
5.eroplane function.. door krey ga.
Array Definition:
Array is big size collection of similar data type items.
Used of Array:
Array can used to store access group data of same data type data type.
For example.
Int array = 120;
Int array = 200;
Int array = 120;
Int array = 130;
Pointer definitions
A pointer is a variable that holds a memory address
where a value lives. A pointer is declared using the *
operator before an identifier. As C++ is a statically
typed language, the type is required to declare a
pointer.
1. Pointer is a variable that stores the address and other variable.:
2. Pointer much more easier and improve your efficiency of the program
3. Pointer store and allow unlimited amount of data :
4. Pointer is used to locate memory dynamically.
5. All operater is working in. int float char..
6. Any variable are constant u must declare a pointer for u can work with it.We
can declare constanct value..
For example and code of Pointer
#include <iostream>
using namespace std;
main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
function parameter
Another example:
//introduction pointer
#include<iostream>
using namespace std;
main(){
int*ptr;
int a;
cout<<"Enter a integer:";
cin>>a;
ptr=&a;
cout<<"The value of a:"<<a<<endl;
cout<<"The Adress of a:"<<ptr<<endl;
}
#include<iostream>
using namespace std;
main()
{
int *ptr a = 10;
cout << "first value is " << ptr a << endl;
}
&array ke sath hm array ke data type dekh sktyn he.. ke int main valule store ke
hey kke ..
Return 0 . data structure ke property hen..
#include<iostream>
using namespace std;
main()
{
Memory for this types of variable is located once. When your program is run and
persists form life the life of your program.
Automatic memory location.
Supports the locate variable and parameter memory for this types of variable
Is located relevant block is enter and … when the block is existed as many time is
as necessary.
Dynamic memory location::
Dynamic memory location is a way for running the program to request the memory
from the operating system. This memory dost not come from the program. Limited
stock memory. Instead it is located from a much large.
Pol of memory managed. By the operating system. Called the heap.
Local variable..
We can give is as much as relevant value.
What is class:
Class is a user defined data type which defined by user. Class is
complete opp based data structure in it we make oriation of the object.
Definition of the Class.
#include<iostream>
using namespace std;
class Student{// class name
public:
int rollno;
string name;
double GPA;
void print(){
cout<<rollno<<"/ "<<name<<"/ "<<GPA;
}
};
main(){
Student a{85 , "AMNA" , 2.5};
a.print();
}
#include<iostream>
using namespace std;
class Student{// class name
public:
int rollno;
int id;
int year;
void print(){
cout<<rollno<<"/ "<<id<<"/ "<<year;
}
};
main(){
Student a{85 , 6 , 2018};
a.print();
}
#include<iostream>
using namespace std;
class Student{// class name
public:
int rollno;
string name;
double GPA;
char gender;
void print(){
cout<<rollno<<"/ "<<name<<"/ "<<GPA<<"/"<<gender;
}
};
main(){
Student a{85 , "AMNA" , 2.5 ,'female'};
a.print();
}
Conceptual Apprach..
Conceptual mean think and approach mean .. last step taak reach and finish.
5 things oop ke sath call hote..encapsulaiotn.Abstraction. polymorphisam.,
inheritance
Reusability.
Encapsulation:
Encapsulation: is a most important of the programing language.
This is used for data hiding ,
#include<iostream>
class somethings {
public :
int a_value1 ;
int a_value2 ;
int a_value3 ;
};
int main()
{
somethings example;
example.a_value1 = 5;
std::cout << "example.a_value1 ";
}
Abstraction class::
Add a new operator for function is used for the this operator.
Void data ko screen pr how krwaney ke liye use hota hye.
What is Template?
Templates are powerful features of C++ which allows you to write generic
programs. In simple terms, you can create a single function or a class to work with
different data types using templates.
Second definition:
Templates are of great utility to programmers in C++, especially when combined
with multiple inheritance and operator overloading. The C++ Standard
Library provides many useful functions within a framework of connected
templates
#include <iostream>
using namespace std;
public:
Calculator(T n1, T n2)
{
num1 = n1;
num2 = n2;
}
void displayResult()
{
cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
cout << "Addition is: " << add() << endl;
cout << "Subtraction is: " << subtract() << endl;
cout << "Product is: " << multiply() << endl;
cout << "Division is: " << divide() << endl;
}
int main()
{
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
return 0;
Structure