Saturday 24 October 2015

Class

# include<iostream>
using namespace std;
class cal
{
//Data Members
int a,b,add,sub,mil,divide;
public:
cal()//Constructor
{
a=b=add=sub=mil=divide=0;
}
//Functions
void addition()
{

 add=a+b;
cout<<"The total is"<<add<<endl;

}
void subtraction()
{

if(a>b)
{
sub==a-b;
}
else
{
sub=b-a;
}
cout<<"Subtraction result  is" <<sub<<endl;

}
void multiplication ()
{

mil=a*b;
cout<<"Multiply is "<<mil<<endl;

}
void division()
{

if(a>b)
divide=a/b;
else
divide=b/a;

cout<<"The Divisior is  "<<divide<<endl;
}
};
void main()
{
cal c;
cout<<"Enter First number"<<endl;
cin>>c.a;
cout<<"Enter Second Number"<<endl;
cin>>c.b;

c.addition();
c.subtraction();
c.multiplication ();
c.division();

}
In this program we will discuss what is class and why we need it??

Class:

class in C++ is a user defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers privateprotected or public (by default access to members of a class is private). A class (declared with keyword class) in C++ differs from a structure (declared with keyword struct) as by default, members are private in a class while they are public in a structure. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. Instances of these data types are known as objects and can contain member variablesconstantsmember functions, and overloaded operators defined by the programmer.

Syntax:

class
{
//Generally,Data Members are kept private ,so that they can only be access inside of class
Data Members;
//Generally functions are kept public so that they can be used outside of the class.
Functions

};

In this program,we are making a simple calculator.There is one class named cal having some data members and some functions(Which are accessed outside of class using object) ....

Constructor:
Constructor is a function which is automatically called by object,when object is initialized ,In constructor we can initialized variables and other related things if require,If the Constructor is not defined,C++ can make constructor itself but body is empty.It is normally used to initialized  variables.



Note that,Here data members are not declared private but they cant accessed outside of class because until we can define any access identifier ,any variable or function will be by-default private,

Any Suggestions or help ,plz comment.
Thanks


Sunday 8 December 2013

Structures

# include<iostream>
using namespace std;
struct student
{
char name[10];
int r;
float m;
};
void main()
{
student s;
cout<<"Enter name"<<endl;
cin>>s.name;
cout<<"Enter Roll number"<<endl;
cin>>s.r;
cout<<"Enter Marks"<<endl;
cin>>s.m;
cout<<"Name is"<<s.name<<endl;
cout<<"Roll number is"<<s.r<<endl;
cout<<"Marks is"<<s.m<<endl;
}

In this program,we will discuss Structures........!!

First,What is the Structure is??Why we need it??

Structures:-
Structures are a way of storing many different values in variables of potentially different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. Structs are generally useful whenever a lot of data needs to be grouped together--for instance, they can be used to hold records from a database or to store information about contacts in an address book. In the contacts example, a struct could be used that would hold all of the information about a single contact--name, address, phone number, and so forth.

I think now,you got is what the structure is......!!

Syntax:-
struct structurename
{
only data types not functions
};

Note:-
before using structure,you will first make it and then use it........!!It will be not make in any function...!!



In this program,we will make a student structure and make dattypes of  name,roll number and marks.....!!
In main,we will make an object of student(structurename) and enter name and other things in it......!!
s.name mean using s we will assign name,and so on......!!

Then,after we will printing name and other datatypes using s.......!!


Here,you will make your own Datatype like int,float but in your datatype student there is one char,one int and one float........!!

I think you got it.........!!!

Feel free to give suggestions in comment box.......! 


Function by Reference

#include <iostream>
using namespace std;
void swap(int &a, int &b);
int main ()
{
  int a,b,z;
  cout<<"Enter First Number"<<endl;
  cin>>a;
  cout<<"Enter Second Number"<<endl;
  cin>>b;
  cout<<"Numbers before Swapping"<<endl;
  cout<<a<<endl<<b<<endl;
  swap(a,b);
  cout<<"Numbers after Swapping"<<endl;
  cout<<a<<endl<<b<<endl;
}
void swap(int &a, int &b)
{
int c;
c=a;
a=b;
b=c;
}

In this,we will discuss sending data by reference......!!
But,First you will knowm what the difference b/w Calling function by Value and
Calling function by Reference......!!
Call by Value:-
1)Call by value passes the value of actual parameter to formal parameter
2)The actual and formal parameters refer to different memory locations.
3)Any change made by function in formal parameter does not affect the value of Actual parameter.
4)It Requires more Memory.
5)It is less Efficient.
Call By Reference:-
1)Call by Reference passes the address of actual parameter to formal parameter.
2)The actual and formal parameters refer to same memory locations.
3)Any change made by function in formal parameter actually changes the value of Actual parameter.
4)It Requires Less Memory.
5)It is more Efficient.

Now,I think you known Whats the difference b/w..............!!!
In this program,we will simply geting two numbers from users and then displaying it,and afterthat we call the function swap() and it will swap these two numbers(mean a is put to b and b is put to a).......!!!
Note:-
If you want to type your function after main,then you will always tell the compiler that a function name this will be using in this program like in this case....!!
What the swap() function is doing.....??
We will send int a,b to it,but on other side(function side) there are pointers which will save there addresses respectively........!!
And its done...!
If we give,
a=2;
b=3;
After swapping it will be,
a=3;
b=2;


I think you got it.........!!!

Feel free to give suggestions in comment box.......! 

Pointers


#include <iostream>
using namespace std;

int main ()
{
  int a = 5,b = 15;
  int *p1, *p2;

  p1 = &a;             // p1 = address of firstvalue
  p2 = &b;             // p2 = address of secondvalue          
  cout<<*p1<<endl<<a<<endl;
  cout<<"*p2<<b<<endl;
  cout<<&a<<p1<<endl;
  cout<<&b<<p2<<endl;
 
  
}


In this,we will discuss pointers......!
So,first we known what are pointers.....!!
Pointers:-
Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need. Wow, that's kind of cool.


In this program,we will take two int,a and b and two pointers,p1 and p2 of int type.....!!
When we assign p1=&a,we will giving p1 address of a and same case with p2.....!!
and then we will printing out *p1,which will print the value which we give to a .....!!
How's this possible.......Actually,p1 is pointing to a(not to its value) and *p1 means what the value of a is........!!

And if we print &a it will print the address of a,and if we print p1 it will also printing the address(These two addresses are same) and same case with p2 and b......!!

I think you got it.........!!!
Feel free to give suggestions in comment box.......!