COLLECTOR :seyed sajjad ale mohammad COMPILERdownloads.nimaarab.com/Nima_Content_Management... ·...
Transcript of COLLECTOR :seyed sajjad ale mohammad COMPILERdownloads.nimaarab.com/Nima_Content_Management... ·...
���
�����
��
� �
� �
��
��
� �
���������C++
��
��������� ��
• �����C++
• ������������ �!��"�����
• #$%&'�(&� ����)%����
• �*��%���
• +����,����- ��."���/�0��1�2 �
���/��3�-���"���3�-�/�2��3�-���&4���-��56�3�-��
• ,��2���-�(��
www.itinahwazpnu.blogfa.com
COLLECTOR :COMPILER :reza sepas yar
seyed sajjad ale mohammad
�����
��
��
�����C++��
��
• ����7��-� �8��������*��4C++�9����:��6��#��;�<
• Brace�9=6���>?@����A�B*�;��:&=C#-����*�%�;�� �8�������9�*�<
• ���������2���,��D�����=�� �/�2��;�� �8�������9�*��4��6�>?@� ��*<
• ����EE���/* … */���2���,��D������F&��%�:�2��� ��*<
• 9=�G-��*��%������.*��H�3�-�&I��������-�/��=2���:&=C#-���9=2�*�,92��������#B6����9���%��#����
=���%��#�9�������#6�9��*��-�4�J�K�����2�+��2� �97���6���6�;���*LM�92�*���6���6<
• C��4�*��;�Case Sensitive���2���1��N����D%�O�P*���;Q�6�R��S�:&*���.��<
• 9��2�,��D����;Q�6�R�S��*�9��*��#�S�,92��������#B6��<�1Tif, char, while,…�� �
������������ ���
• /U0�S�������)� ��*���V�G�W���X�?�����=V���&I�����V%�
• �&I�����V%�W�0��������� ;��&I��+�����&I��W����������������J�T�������������������������� char a ;���
• .*�H����V%�W�0��������� ;����9Y�Z.*�H�+���.*�H�W��������J�T���������������� float pi=3.14;
��
��
��
���
�����
��
���������������
���� ������ ������� �����
����� ����������������� ������� ������ �����������������
�� ��������������
���� ��� ������������� ������ ���������������������
�� ����������������
��� �������� ����� ��������� � ��� ������ � ��� ��
�� ���������� �! !���!��
���� ��� "��� ������� ����� ��������� � ��� ������ � ��� ��
�� ���������� �! !���!��
��� #�����$����� ������ �������%����
%���� &��� ���' ���������� ����� �( ��)*�����+��� � �,�
������ -�����'��� ��%��� ���' ��� ������ �(���)*������+���� � �,�
��
������������������� !���
� ��������"#�$�%& !� ���
�[B#7����)B#7��J�T��/\&����
.��X�����.�����
*�'&GY%���*���( �
)��#!���)��!�
��]��D%��������
/�,9��#&N�*����/����
0�12-��3&��0��3�&��3���
4�OR �3���4��3����3���
5�XOR �3�&�5��3&&��3&��
6��;��1#)��6+�3&�,��3�&�
77���/*�.D&2.��� �3&��77� ��3�&�
88��^Q�/*�.D&2 �3�&�88� ��3&��
�����
��
��
���'��$�%& !��
�[B#7����)B#7��J�T��/\&����
���/=��N��-a a = a× -1
))�9S���;��_��P0���a++ a = a + 1��
����9S���;��_-�6��a-- a = a - 1
��
$��("��)��$�%& !��
�[B#7����)B#7��J�T��/\&����
7����(�P*���7��&����
8���)Q�6��9�979�9��:����
;7� ��G������(�P*���7;��:����
;8� ��G������)Q�6���(�8; �&����
;;�� ��G%��919�;;9#9�&����
<;�� ��G�����<;��:����
��
�)*+��$�%& !��
�[B#7����)B#7��J�T��/\&����
00��12-��Y�=��+�7�,00+�<;�,�&����
44�=> ��Y�=��+9�97�,44+�8�(�,�:����
<�`&Y����7�,<+��&����
��
,�" ���$�%& !��
�[B#7����)B#7��J�T��/\&����
;��X�G������;��������
���
�����
.;�X�G������X�����.;���;�.��
*;�X�G������'&GY%���*;���;�*��
);�X�G�������#!���);�����;�)��
;�X�G������]��D%����;���;����
�;+$����,?3����K�2�X�G�����
��(�value��a&Fb���9Y
�9ccc2�*a�ccc*��*��x������
��*��*����$=���&dy���
��2<��
� �
��
��
�-�.���$�����/��� ����
�����75K��.0����� ��*�����/6����9������ ����PC�9&B6�/FDb�92�*���������cin����75K��J����� ��*��
�/*92�*����[@��#��/6����9�������!��"�������cout�92�*�����!�<��
�-�.��� ���cout��
cout << "Hello World";
�����7�e�0������This is a test�9=6���f�Q�_��#��/FDb� ����*���<��
cout << x;
��&I�������F�e�0������x�9���0���_��#��/FDb�/*���<�
�.57�����[B#7insertion�,��*��������*�;�����_&*�.���:)#������7�;�J�T�4��=7�/*�3������6�/* ��
�����
cout << "Hello" << "World!";
��[B#7insertion�9-�����-�&I�����-�/�2���&6�%�4�)�<��
int age=25;
cout << "My age is:" << age;
My age is: 25
�.57����9�9!�g"��-�/*�J�Y���� ��*n�����endl�'&=6���,��D����<������������J�T�4��=7�/* �
cout << "First Line.\n";
cout << "Second Line.\n Third Line.";
First Line.
Second Line.
Third Line.
cout << "First Line." << endl;
cout << "Second Line." << endl;
First Line.
Second Line.
��
�
�$������� ��cin��
������cin��.57�,��#-�����������6�/*�<��
int age;
cin >> age;
�������;��g��%cin��4��%���&I��;�����_&*�6��-����9Y������<��[�9)��J��V�������������J�T�4��=7�/*
9=2�*��� ��
���
����
cin >> a >> b;
��
�$�(�����$�-�Hello World����Visual C++ 6.0��
��
�� /���*� ��!�����hi�File\New\Win32 Console Application�9&=6�X�?������<���X�?����j�0��*
,k��i��&G���W���
�� 1b�S����6����An Empty Project�9&=6�X�?������<
� �&G�File\New\C++ Source File��/6�9��2�:l#����,��6��K���Add to Project��,92�X�?���
.��<
m< �9��Gi��*�1��0���cpp�9&=6�������������96 � �
#include <iostream.h>
int main()
{
cout << "Hello World!"<< endl;
return 0;
}
��
��9�*�F7��h����Ctrl+F5���*�9-��"��������b�/*�/\&���39&=6�;&B6��� ��
rld!
Press any key to continue 9=6���X�GS����,�����;��.S�G���g&F����� �/���*< �
��
#include <iostream.h>
����
int main()
{
int r;
const float pi=3.1459;
cout <<"Enter Radius: ";
cin >>r;
cout <<"If radius="<<r<<" Circumference is:
"<<2*pi*r<<endl;
cout <<"If radius="<<r<<" Area is: "<<pi*r*r<<endl;
return 0;
}
��
��
��
��
��
��
�����
��
��
��'����$�0�1� 2���
��
��� ��goto� �
��2���W�\����GQ�*�;��/*�n�2�4�9*���i<���97����� �/���*�J�T�4��=7�/*Mo���%o��W�&i�.��8��������Db
End!���#����9-����_<��
#include <iostream.h>
int main()
{
int n=11;
loop1: n--;
cout<<n<<endl;
if(n!=0)
goto loop1;
cout << "End!"<<endl;
�������
return 0;
}
��
��
�� .��else �3�if���
if(���)
{
������� ;
}
else
{
������ � ;
}
� b����4��%���9=2�*���"�;����������/6��%��brace���#��RpS���<
� �A5*����,��D���else�.��� ��&�"�<
� �
9=6���X�GS����W���/!��� �/���V� �-�/@������� �/���*�J�T�4��=7�/* ��
#include <iostream.h>
#include <math.h>
int main()
���
�������
{
float a,b,c,delta;
cout <<"Enter a, b and c: "<<endl;
cin >>a>>b>>c;
delta=b*b-4*a*c;
if(delta>=0)
{
cout <<"X1="<<(-b+sqrt(delta))/(2*a)<<endl;
cout <<"X2="<<(-b-sqrt(delta))/(2*a)<<endl;
}
else
cout<<"Roots are not real!"<<endl;
return 0;
}
��
��� .��Switch - Case���
switch( ����� )
{
case ������ :
������ ;
break;
case ����� :
����� ;
break;
.
�������
.
default:
n�����;
}
�
9=6���/���F����/\&���,92�X�?�����*�%�������*���/�0�(��*��6��������97�;������ �/���*�J�T�4��=7�/* � �
��
#include <iostream.h>
#include <math.h>
int main()
{
float num;
int operation;
cout <<"Enter a number"<<endl;
cin >>num;
cout<<"Enter 1, 2, 3 or 4 to seleting sin(), cos(),
tan() or Log()"<<endl;
cin>>operation;
switch(operation)
{
case 1:
cout<<"sin of "<<num<<" is "<<sin(num)<<endl;
break;
case 2:
���
�������
cout<<"cos of "<<num<<" is "<<cos(num)<<endl;
break;
case 3:
cout<<"tan of "<<num<<" is "<<tan(num)<<endl;
break;
case 4:
cout<<"log of "<<num<<" is "<<log(num)<<endl;
break;
default:
cout<<"Enter 1, 2, 3 or 4!"<<endl;
}
return 0;
}
��
��
��� .��While���
while(���)
{
�����;
}
�
���6���6�.���"����*��6����3q��"� ��*����� �/���*e�9=6�����<��
�
#include <iostream.h>
�������
int main()
{
char c=0;
while(c!='e')
{
cout<<"Enter e to exit"<<endl;
cin>>c;
}
return 0;
�
�
��
.�����Do/While�� �
���.��S�:����R5"��* While�1N�9S�3/YBS�1"�����������:���*�=*�3��2���_����/YBS� �8�������n�2
9��2�����!�����)�<
��
do{
�����;
} while(���)
�
���
�������
��
�$��()&#For���
for( ������������������ ; ���������� ; ���)
{
�����;
}
� �
�����97�;������ �/���*9=6���X�GS����4�1����6�0���/�0�(��*��6���<��
�
#include <iostream.h>�
�
int main()�
{�
�
int i,n;�
double fact=1;�
�
cout<<"Enter a Number: ";�
cin>>n;�
�
for(i=1;i<=n;i++)�
fact*=i;�
�������
cout<<"Factorial of "<<n<<" is "<<fact<<endl;�
�
return 0;�
}
�
�
�
�
�
�
�
�
�
�
�
�
��
���
������
��
4���� ��
����V%92�*����������b�/*��*��% ��
�*�%� �8���(�����!��"�,����+���������*�%�W�� )�
{�
�BF� �-�&I��
� ��*�%�������� �
}�
��
J�T �9=6�f�Q����4��V)���,��6�.0���������97�;��/6�9&G��=*� ��/���*<��
�
#include <iostream.h>�
�
long int cube(int x){�
return x*x*x;�
������
}�
�
void main(void){�
int a;�
cout<<"Enter a Number: ";�
cin>>a;�
cout<<"Cube of "<<a<<" is: "<<cube(a)<<endl;�
}
��
��9���(����V%�9��*�'-�����9!���9=2�*��#�����V%�1*�N��[�9)��1"����*��%�<��
��
TJ�� 92�.0������97����'#�P6��/6�9&G��=*� ��/���*9=6�f�Q����,<��
#include<iostream.h>�
�
int _max(int a,int b){�
if(a>b)�
return a;�
else�
return b;�
}�
�
void main(void){�
���
�������
int a,b;�
cout<<" Enter Tow Numbers: "<<endl;�
cin>>a>>b;�
cout<<"Maximum of "<<a<<" & "<<b<<" is
"<<_max(a,b)<<endl;�
}
�
���*�%�4��%����*�%����9V*� ��� �+,���/���*� �9�*�� ���9��*��*�%� �/��=2�/6����D%�:��� �*� 3��#���0�V�P&�
��2����V%�<.2���4��%���P&���������b�/*����1�N� �/���*�J�T�4��=7�/* ��
#include<iostream.h>�
�
int _max(int a,int b);�
�
void main(void){�
int a,b;�
cout<<" Enter Tow Numbers: "<<endl;�
cin>>a>>b;�
cout<<"Maximum of "<<a<<" & "<<b<<" is
"<<_max(a,b)<<endl;�
}�
�
int _max(int a,int b){�
if(a>b)�
return a;�
else�
return b;�
�������
}
��
������1��0�;�����4�457�����P\� �/��?*��6�;������*��%����V%��*Header��;#6�/���*�W�\G���/*�4��%��
��#�<��
�� ��5�6��$����#
/6�/���*������#GN��&I���,92����V%�4��������&I��4�,��S9=��(���<���C��;���*�/6�/���*������#GN�/*
�.D!#��������.���,92���$F5*�;�=��(���A9<���������1*�N�,92����V%�A5*�4�#-�����&I��;�
*��#��,��D����1*�N�A5*����q��"���������,��*92�<���
�W�I&i�����/���*����J�T�4��=7�/*����������� ���� % �����6�'&-��"�.0�������<��
�
#include <iostream.h>
void main( )
{
{
int x= 1;
}
x=x+1;
}�
�������
��2������V%��*��%�/#-����q��"�/6� �&I����,92�/�"�=2�3�*��%�W�#%� ��*�92�*���,��D����1*�N�</*��4��=7
��&I������ �/���*����J�T3�92�*�����&V���:&=Q���<��
�
#include <iostream.h>�
�
int x=5;�
int test();�
�
void main(){�
cout<<x<<endl;�
cout<<test()<<endl;�
}�
�
int test(){�
return x;�
}
�����"���9Y�3�*�%��BF��&I��;��'&-��?*��(��9V*� �-�����"��0� ��*P&�� �6�rDS39=�C��/#B6� 9&B6�
static�����N�����&�"�������9-����<�
static int x=s;
�4�9*�������� �/���*��(��J�T�4��=7�/*static���97�,���#-��!��"�'&=6���!�Mo���*�9-��"<��
#include <iostream.h>�
�������
�
int test();�
�
void main()�
{�
cout<<test()<<endl;�
cout<<test()<<endl;�
}�
�
int test(){�
static int x=10;�
x = x*2;�
return x;�
}�
4�����78�9�:�$�;����0�<�
��#�����V%�j�0�_&i�4��=7�/*�������-�4��(���*��%� ��*�4��%���<�4��(��3�*��%�����"��0����/6���[=-
�4�j�0�_&i���9Y�392�*�,92�RpS�j�0�_&i���9Y� ������(�43���*�%�/*�J�����9-��"29(�J�T�4��=7�/* ��
�
#include <iostream.h>
double volume(double r=1, double h=1);
double pi=3.145;
void main(){
cout<<"The default volume is: "<<volume()<<endl;
�������
cout<<"The volume with r=10: "<<volume(10)<<endl;
cout<<"The volume with r=10 h=5 is:
"<<volume(10,5)<<endl;
}
double volume(double r, double h){
���return pi*r*r*h;
}�
�
��
���#�=�'>��%& !��Scope Resolution Operator���
'&������/6������#-������#7��&I���*�W�=#-��BF��&I��4��%���3� �,��9F��#�����V%��*�%�;��<�/*�/!�%��*
:����/Q���#7�W�=#-��&I��/*�������� ��*��*�%�;�� �,��9F����/6�.���:�����2���t���/6�������/BlG
���U=�:���/*�u�������!�� ��)-��C��/=���;&)D%����[���[B#7��������.���,�������N�����&�"�����<�/���*
��[B#7���*��6�,�F����������9-����4�@����<��
�
#include <iostream.h>�
�
int amount = 123; // global variable�
�
void main()�
{�
�������
int amount = 456; // local variable�
cout << ::amount << endl; // Print the global
variable (123)�
cout << amount << endl; // Print the local
variable (456)�
}�
��
��
Overload�4�����?�@��
���C++��1#7�:���/*�/6���#�����V%�4�G)��W����*��*�%�9=Q�/6��������!��4�)��:��=$����� ������/�D(
��2�<.����-�4�/*�������� �-�4�(�� �,����+����������P��#���[�9)���������*��%�/C�<��
��
�
#include <iostream.h>�
�
int abs(char i);�
int abs(int d);�
int abs(long l);�
�
int main(){�
�
cout << abs(-10) << endl;�
cout << abs(-11) << endl;�
cout << abs(-12) << endl;�
return 0;�
�������
�
}�
�
int abs(char i){�
return i<0 ? -i : i;�
}�
�
int abs(int d){�
return d<0 ? -d : d;�
}�
�
int abs(long l){�
return l<0 ? -l : l;�
}�
��
�.�8��-������4���������
�,92�J�������9Y�/6��������!��4�)��:���'���6�,��D�����*�%�/*�:��(��J����� ��*�4�=6�%�/6��2���R5"��*
9=6��&&I%�4�g��%��*�%�/*�<��&I��4�������2�J������*�%�/*��&I�������6�;��/)�� �!�/*��[�������7��*
���,92�J������&I����",��D����������&(������N��*�%� �<J�T�4��=7�/* ��
#include <iostream.h>
�������
void inc(int &x,int &y);
void main()
{
int a=1 ,b=2;
cout<<"a = "<<a<<" and b = "<<b<<endl;
inc(a,b);
cout<<"a = "<<a<<" and b = "<<b<<endl;
}
void inc(int &x , int &y)
{
x++;
y++;
}
��
�����@(��*���9Y�;�����_&*�9���%����*�%�1�N�����R5"�*���2���/US5�/���*�:������/6������#-
92�*�/�2��<���
��
��
��
��
��
��
��
������
��
��
��
����������( 8����.���$��
��
AB ��(���<��
��(���<�?C!�
J�K�v,����+�����/����W�� ];�
�-����9Y��������b�/*�4��%������/�����b�=7���#��<�/*����4��B����6�3��@��/�2���/����J�K�/6��%��b���
��&(����U�����,92�,����>&$?%��b�=7���9V%<��
int a[5] = {1, 2 , 3, 4, 5}�
�
J�T� �/6� ��/���*Mo�9=6���'�������������#���-�4� ��*���/�0�(��*��6��������97<��
��
�
#include <iostream.h>�
�
void main()�
{�
int num[10];�
������
int i,j;�
�
for(i=0;i<10;i++){�
cout<<"Enter Number "<<i+1<<": ";�
cin>>num[i];�
}�
cout<<endl<<"Index"<<" Number"<<endl;�
�
for(i=0; i<10;i++){�
cout<<i+1<<"\t "<<num[i]<<"\t";�
for(j=0; j<num[i]; j++)�
cout<<'|';�
cout<<endl;�
} �
} �
�
��
��( D���
�4�*����C�9��2��� ����,��&i��-��6���6���� ��/�����*��-�/�2�<��
char str[]="Test";
�/#B6�J�K��*�.����*��*�e�0�/����J�K:����n�*��/6�9S���;��,�5V*���6���6�/*2�������@��.�����392�*��
�6����V%�P&���������b�/*����4�4��%���:���*�=*�<��
char str[]={'T','e','s','t',\0'}
�������
��������*� ��9���"� ����������� ��/�2������Y�4��%��<
#include <iostream.h>�
�
char str[10];�
�
void main(){�
cout<<"Enter Your Name: ";�
cin >> str;�
cout<<"Hi "<<str<<" !"<<endl;�
}
��
��"�9=��#-�/�2��;�� �-��6���6�;%�;%�/*.2����������4��%���/����;�� �-�/ ��
�
#include <iostream.h>�
�
char str[10];�
int i;�
�
void main(){�
cout<<"Enter Your Name: ";�
cin >> str;�
�
for(i=0;str[i]!='\0';i++)�
���
�������
cout<<str[i]<<endl;�
}
��
�
w< �-���"�����-���6���
=7���,��*�/�0���."��� �,����+���:&����-���"��9=���%������"���;���b�� �-�+������ �,������B�?
9=2�*�<9-����4�@��������"���;�����V%� �,�F������J�T<�
��
struct studentRec�
{�
char studebtName[16];�
float midTermGrade;�
float homeworkGrade;�
float finalGrade;�
float aveGrade;�
};
�/#B6�e�0���������struct�;���W����*�������"��studentRec� �����,92���\������"���9=6�����\���s���$=7
�/6�92�*���/&Y*���/�2��+�������-�4�����)� ��@7��9=2�*���<��
�9!� �,����+���;�����V%��N���������"���;��457�9��:���*�=*���.������hi���"�����\����:������'&���%��
6�,��D����9�9!�,����+��'&=6����V%�+���:������������-�&I����'&=�<��
studentRec firstStudent;
studentRec Student[5];
�������
��
�(���� ��$�E!���� .��
�/�Y���[B#7�������"���;���b�=7�/*��*�&���� ��*�<��'&=6���,��D����<�;���$=7�/*��*�&���� ��*�/�Y���[B#7
�D��������3�&I��;��W���]��K�������"��J�T�4��=7�/*�3��&(������N�,����
firstStudent.finalGrade = 17;
J�T� 9-����4�@������-���6��;���-����9Y������V%� �,�F������ �/���*<��
#include <iostream.h>�
�
struct studentRec�
{�
char studebtName[16];�
float midTermGrade;�
float homeworkGrade;�
float finalGrade;�
float aveGrade;�
};�
�
studentRec firstStudent;�
�
void main(){�
�
cout<<"Enter Student Name: ";�
���
�������
cin>>firstStudent.studebtName;�
�
cout<<endl<<"Enter Mid Term Grade: ";�
cin>>firstStudent.midTermGrade;�
�
cout<<endl<<"Enter Homework Grade: ";�
cin>>firstStudent.homeworkGrade;�
�
cout<<endl<<"Enter Final Grade: ";�
cin>>firstStudent.finalGrade;�
�
firstStudent.aveGrade = (firstStudent.midTermGrade
+firstStudent.homeworkGrade+firstStudent.finalGrade)/3;�
�
cout<<endl<<"The Average Grade of
"<<firstStudent.studebtName<<" is
"<<firstStudent.aveGrade<<endl;�
�
}�
��#�������i�������6�� �������9V%��U���������"���+������ ��/�������V%��*�4��%�� ��
#include <iostream.h>�
�
struct studentRec�
�������
{�
char studebtName[16];�
float midTermGrade;�
float homeworkGrade;�
float finalGrade;�
float aveGrade;�
x�
�
studentRec Student[5];�
�
void main(){�
int i;�
for(i=0;i<5;i++){�
�
cout<<"Enter Student Name "<<i+1<<": ";�
cin>>Student[i].studebtName;�
�
cout<<endl<<"Enter Mid Term Grade: ";�
cin>>Student[i].midTermGrade;�
�
cout<<endl<<"Enter Homework Grade: ";�
cin>>Student[i].homeworkGrade;�
�
cout<<endl<<"Enter Final Grade: ";�
cin>>Student[i].finalGrade;�
�
�������
cout<<"-------------------------"<<endl;�
}�
�
for(i=0;i<5;i++)�
Student[i].aveGrade = (Student[i].midTermGrade�
��+�
�
� Student[i].homeworkGrade+Student[i].finalGrade)/3;�
�
cout<<"Name\t"<<"Grade"<<endl;�
�
for(i=0;i<5;i++){�
�
cout<<Student[i].studebtName<<"\t"<<Student[i].aveG
rade<<endl;�
}�
�
cout<<"-------------------------"<<endl;�
}�
�
�
�
L< Union��-
�������
�/�0���."��� �,����+���:&��Union�2�*��9�<����"���/&�2�,����+���:��,��*�4������/6����D%�:����*�3
����V%��/��=2�;����gY0�����2��#��/�0�(��U��������"����y7��W�#%� ��*����y0��-���"���R5"�*�3+���:��
��2������V%����/U0�S����/K�*�� �y0��&I��4����,��D����4������<����V%Union�������"���9=��#-
92�*<��
��
union studentRec�
{�
char studebtName[16];�
float midTermGrade;�
float homeworkGrade;�
float finalGrade;�
float aveGrade;�
};
����"���;��9=��#-�;���b�=7�/*��*�&���� ��*Union��/�Y���[B#7����<��'&=6���,��D����<�hi�/6����D%�:����*
��$=7��-��-����9Y���Union��������:&*�����B�N���9Y<��
studentRec.homeworkGrade = 15;
studentRec.finalGrade = 18;
studentRec.aveGrade = 17;
��
��
��
��
�������
��
��
m< �-��56
FC@�5�6�� �
��C++�9������!�*���\� �,����+������V%�4�)���-��56� �/B&��*�<������V%��-���"���9=��#-��8�56
�/6����D%�:����*�9��2�8�56���3��4��=7�/*�9=���%���P&���*��%�y7�9=2�*��56.�/#B6�g��%��56�;� �
9&B6�class���2������V%<��
�
class TimeType{�
�
public:�
void setTime(int,int,int);�
void Increment();�
void Decrement();�
�
private:�
int hour;�
int minute;�
int second;�
}�
�
������
����9V*�/6��V*�%���� ��,�����y7��-'� $�����92�*����*�&����1*�N��56��y7��*��%� ��*��8=%���2������V%
9=2�*� �#�� �������1*�N� /���*� 1"��� ��� '&Y�G� ��K� /*� �� < �y7� �*�%�/6���*�%� ����� ��!���56� �*� W�=#-
�����������2���,9&���<,9������*�%��y7��*�%�3,���� �y7��/&��������Y�/6�.����b�$?*� ���:&&V%���
9=6��� <�2�/���*�/6���[=-�� ���� ������(�������"��0���6��"���K�/*�,9������*�%� 39=6�����\����56�3
9����(��#���*���� ��9Y���,��6�/&�����-���9Y�����-�����i<��
�56� /6� ��[=-� 3� �-���"��� 9=��#-:�'�: ������V%� �56� +��� ��� ��� ���&2�� 4��%� �� 9���(� ��
:�'�: ����6���\��<��
TimeType GlobalTime;
TimeType TimeArray[5];
J�T
#include <iostream.h>�
�
class Cube�
{�
public:�
Cube();�
void setSide(double s);�
double getSide();�
double Area();�
double Volume();�
void Properties();�
private:�
double Side;�
};�
������
�
�
void main()�
{�
int side;�
�
cout<<" Enter side of cube: ";�
cin>>side;�
cout<<endl;�
�
Cube MyCube; �
�
MyCube.setSide(side);�
MyCube.Properties();�
�
}�
�
//----------------------------------�
�
Cube::Cube()�
{�
Side=0;�
}�
�
void Cube::setSide(double s)�
{�
�������
Side = s <= 0 ? 1 : s;�
}�
�
double Cube::getSide()�
{�
return Side;�
}�
�
double Cube::Area()�
{�
return 6 * Side * Side;�
}�
�
double Cube::Volume()�
{�
return Side * Side * Side;�
}�
�
void Cube::Properties()�
{�
cout << "Characteristics of this cube";�
cout << "\nSide = " << getSide();�
cout << "\nArea = " << Area();�
cout << "\nVolume = " << Volume() <<endl<<endl;�
}�
�
�
���
�������
���,��#�����V%� �[���1��0�����P\����b�/*����4�'&���%����56�;��4��6�/��G�6���/���*�W�\G��� ��*
�1��0��������56� �[��Header���#�����V%<��
��
�9��Gi��*��Bb��1��0.c
#include <iostream.h>�
#include "cube.h"�
�
void main()�
{�
int side;�
�
cout<<" Enter side of cube: ";�
cin>>side;�
cout<<endl;�
�
Cube MyCube; �
�
MyCube.setSide(side);�
MyCube.Properties();�
}
�
�
�������
�
�1��0�����56����V%cube.c
#include <iostream.h>�
#include "cube.h"�
�
Cube::Cube()�
{�
Side=0;�
}�
�
void Cube::setSide(double s)�
{�
Side = s <= 0 ? 1 : s;�
}�
�
double Cube::getSide()�
{�
return Side;�
}�
�
double Cube::Area()�
{�
return 6 * Side * Side;�
}�
�
�������
double Cube::Volume()�
{�
return Side * Side * Side;�
}�
�
void Cube::Properties()�
{�
�
cout << "Characteristics of this cube";�
cout << "\nSide = " << getSide();�
cout << "\nArea = " << Area();�
cout << "\nVolume = " << Volume() <<endl<<endl;�
}
�
��
�1��0�����56�457�cube.h
class Cube�
{�
public:�
Cube();�
void setSide(double s);�
double getSide();�
double Area();�
double Volume();�
void Properties();�
private:�
���
�������
double Side;�
};
��
�FC@�$�E!��(���� �����FC@����#
�,�����y7� �-��56�;��9���,92����V%��56����/6����-�&I�����56��y7��*��%����V*��%���56����/6
����V%9���,92��]BV���56�:���,��S�/*�����,��S� �����39=2�*��#���56��y7�/6��V*��%���9=2�*��� �1��0
9=2�*��<��4��%�����9=2�*����������1*�N��56��y7��*��%�g��%��56� �y7��/#-�3�56�;��,��S���
��6�/V!����8��/*� �#&Y�G�4�@���g��%� <���4��&*�����56� �y7��/*�������� ��*��56�,��S���[B#7���
�/�Y��B��'&=6���,��D���<��39=2�*����*�%� ,��S� ����� 39���,92����V%��56��y7��*�%�;�����/6����-�&I�
9���,92�/�"�=2��*�%�4�#-� ��*�gY0��=V��<��56�,��S� �����/6� �&I��W����*�W�=#-� �&I��3 �y7��*�%�����(�
V%�.����#��'&Y�G���K�/*��*�%�1"������92�*����56�,��S� �����/6� �&I�����b�:������392�*�,92����
��[B#7�9��*�4�/*�������� ��*�����6�+�!��4��%���'��>���� �������������N��&I��W������1�N����<�
��
�E!��(���� ���� +@
��=V�� �y7��/*�������� �80�V'��� ����'� $���3������56��y7��*��%� �� ,���� �y7�� /*�������� ,�F�
9==6���J��=6�<.��S����56�����y7��*��%���,���� �y7��/*�������� ��*�j�0�_&i'� $������p��392�*��
���/�0�(��U������b�$"�39��� ,9�R�V��GQ�*�:&�������1�N����56�W���:&&V%����9V*�/6����y7��/#-
9��2�<R�V��-����hi���� 9V*��GQ�*��%��y7��/#-�/*�39=6���:&&V%�/K�*��R�V�/6�����S�3x��/*�n�*�
�������
J�#7���56����V%� 4���i���2� �� <�y7� �*��%� ]��K� ��� gY0��56�;�� �b�$"� �y7���1*�N��56� 4
9=2�*����������1*�N�/���*�����V*�%��-�g��%��56���#7� �y7��1*�Y����9=2�*���������<�
��
�G����;�C@�$�H����� �����I��
�����Bb���56������������,92�]�@��56������ $������9=��(���<��56�;�����V%� �,�F�
92�*����������b�/*�,92� �*�z�� ��
class derived-class-name : access base-class-name
{
// body of class
};
J�T�4��=7�/* ��
#include <iostream.h>
class BaseClass {
int i;
public:
void set(int n);
int get();
};
�������
class DerivedClass : public BaseClass {
int j;
public:
void setJ(int n);
int mul();
};
void BaseClass::set(int n)
{
i = n;
}
int BaseClass::get()
{
return i;
}
void DerivedClass::setJ(int n)
{
j = n;
}
int DerivedClass::mul()
{
return j * get();
}
int main()
{
DerivedClass ob;
�������
ob.set(10); // load i in BaseClass
ob.setJ(4); // load j in DerivedClass
cout << ob.mul()<<endl; // displays 40
return 0;
}
�
�
�
�
�
�
�
�
�
�
���
������
�
�
��
��-�(�,��2��A �������
��
0����D��5�6�
,��2�� �,����+���9=6��� ��98[����"�������/U0�S� �-�/��"�����3�(�<�;��/*�'&Y�G���K�/*��&I��;��W��
9=6���/V!�����9Y�;��/*�'&Y�G��&d���K�/*��(�,��2��;�����9=6���/V!���3��9Y�<=7�/*�����������J�T�4��
�&I�����+�������� �����&I���'� �(�,��2������+����� ���9=6������V%(��
int a , *p ;�
������[B#7�0���
�[B#7�:���9����(����*������"�9��B#7�/U0�S�/��"�����<J�T�4��=7�/* ��
#include<iostream.h>
void main()
{
int a=10;
int *p;
���
������
p=&a;
cout<<p<<endl;
cout<<*p<<endl;
}
0����D��J����4����(��?���0�<��������
� ��*�������*�%�1"������,92�J������&I��9=Q����;���&&I%� ��*�9=���%���+�!��� �8���(��9=����-�(�,��2�
9��&(����N�,��D���������*��%�/*�O�P*� �-�,����J�����<J�T�4��=7�/* ��
�
#include<iostream.h>�
�
void swap(int *x, int *y);�
�
int main(void)�
{�
int i, j;�
i = 10;�
j = 20;�
�
swap(&i, &j); /* pass the addresses of i and j */�
�
cout<<"i is: "<<i<<endl;�
cout<<"j is: "<<j<<endl;�
�
�������
return 0;�
}�
�
void swap(int *x, int *y)�
{�
int temp;�
temp = *x; /* save the value at address x */�
*x = *y; /* put y into x */�
*y = temp; /* put x into y */�
}�
��
J�T ��92�����*��*��%�_?*����/6������#-C++���-��(�,��2�����,��D����4�9*����+�!����*�4��(��J�����4�)�
9-����<�pi�4�)���������/*�+�!����*�4��(��J�����:���*�=*92�*�����<����/*��������:��� �/G��Y� ��*
9&=6�/!�%����� �/���* ��
�
#include <iostream.h>�
�
void neg(int &i); // i now a reference�
int main()�
{�
int x;�
x = 10;�
cout << x << " negated is ";�
neg(x); // no longer need the & operator�
cout << x << "\n";�
�������
return 0;�
}�
�
void neg(int &i)�
{�
i = -i; // i is now a reference, don't need *�
}�
�
�
#include <iostream.h>�
�
void neg(int *i);�
�
int main()�
{�
int x;�
x = 10;�
cout << x << " negated is ";�
neg(&x);�
cout << x << "\n";�
return 0;�
}�
�
void neg(int *i)�
{�
*i = -*i;�
}�