Professional Documents
Culture Documents
C++ Lab4 - 2018-2019
C++ Lab4 - 2018-2019
Headlines:
#include <iostream>
using namespace std;
class In
{
public:
In (int a ) : n1(a) , n2(2+n1) { }
void dis( ) { cout<<"n1= "<<n1<<" , n2= "<<n2<<endl; }
private:
int n2;
int n1; }; // End of class In
2018-2019 Page 1 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
int main ( )
{
In i(12);
i.dis();
return 0;
} // End of main function
#include <iostream>
using namespace std;
class In
{
public:
In (int a ) : n1(a) { }
void dis( ) { cout<<"n1= "<< n1<<endl; }
private:
int n1;
}; // End of class In
int main ( )
{
In i(12) ;
i.dis ( ) ;
return 0;
} // End of main function
2018-2019 Page 2 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
int main ( )
{
char x = '1';
Don’t try this code for( ; x<=300 ; cout<<x++<<endl);
return 0;
}
const keyword
#include <iostream>
using namespace std;
class In
{
public:
int x =10 ;
void display( ) { cout<<"x = "<<x<<endl ; }
}; // End of class In
int main ( )
{ // will be written in the lab
return 0;
}
2018-2019 Page 3 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
#include <iostream>
using namespace std;
int main()
{
int *pointer ;
pointer = new int ;
cout << "The value of the pointer " <<pointer<< endl;
*pointer = 24 ;
cout << "The value of the int " <<*pointer<< endl;
delete pointer ;
return 0;
} // End of main function
Don’t do this.
2018-2019 Page 4 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main()
{
int *pointer = NULL ;
cout << "Enter the number of subjects : " << endl;
int input ;
cin >> input ;
pointer = new int [ input ] ;
int temp ;
int total = 0 ;
for ( int counter = 0 ; counter < input ; counter ++)
{
cout<<"Enter degree ( "<<counter+1<< " ) "<<endl;
cin>>temp ;
*(pointer + counter ) = temp ;
total += temp ;
}
cout<<"The sum of the degrees is : "<<total<<endl;
double av = (double ) total / input ;
cout<<"The average is : "<< av <<endl;
delete [ ] pointer ;
return 0;
} // End of main function
2018-2019 Page 5 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
Copy constructor
#include <iostream>
#include<string>
using namespace std;
class Test
{
public:
Test( string n , int le )
{ name = n ; level =le ; }
void display ( )
{ cout<<"Student name: " <<name<< endl;
cout<<"Level: "<<level<<endl;
}
void setNL ( string nn , int lee )
{
name = nn ;
level = lee ;
}
private:
string name;
int level;
}; // End of Test class
int main ( )
{
Test t1("Elham" , 3);
t1.display( );
Test t2 =t1;
t2.display( );
t1.setNL("Aya" , 1);
t1.display();
t2.display();
return 0;
} // End of main function
2018-2019 Page 6 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
#include <iostream>
#include<string>
using namespace std;
class Test
{
public: What will happen if we use
Test( string n , int le )
dynamic allocation memory in
{ name = new string (n);
program 4-A.
level =le ;
}
void display ( )
{ cout<<"Student name: " <<*name<< endl;
cout<<"Level: "<<level<<endl;
}
void setNL ( string nn , int lee )
{
*name = nn ; level = lee ; }
private:
string *name;
int level;
}; // End of Test class
int main ( )
{
Test t1("Elham" , 3);
t1.display( );
Test t2 =t1;
t2.display( );
t1.setNL("Aya" , 1);
t1.display();
t2.display();
return 0;
} // End of main function
2018-2019 Page 7 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
Destructor
#include <iostream>
using namespace std;
class Test
{ public:
Test( ) {cout<<"Constructor -> Lab 4 in c++"<<endl;}
Test (int a)
{ num = a;
cout<<"Constructor -> your level is "<<num<<endl;
}
~Test ( ){ cout<<"Destructor is called / num ="<<num<<endl;}
private:
int num = 0 ;
}; // End of class Test
int main( )
{
// will be written in the lab
return 0;
} // End of main function
2018-2019 Page 8 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
Static keyword
#include <iostream>
using namespace std;
int stest( );
int main( )
Static local variable
{
int result = 0;
for (int i =0 ; i++ <= 3 ; result = stest( ))
{ cout<<"The result is "<<result <<endl ; }
return 0 ;
} // End of main function
int stest ( ) What is the output when we
{ delete static keyword from
static int x =2; the program?
x+=2;
return x ;
} // End of stest function
#include <iostream>
Static local variable
2018-2019 Page 9 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
int a =1 ;
Static data member
static int b ; };
// End of StaticTest class
int StaticTest::b=20;
int StaticTest::c=40;
int main( )
{
// will be written in the lab
return 0;
} // End of main function
#include <iostream>
using namespace std;
int main ( )
{
int a [ ] {12 ,8 ,4, 6,10} ;
for ( int i = 0 ; i < 5 ; i++)
{cout<<a[i]<<"\t"; }
cout<<endl<<"------------------------------"<<endl;
for( int e : a )
{ cout<<e<<"\t"; }
return 0; For each -> c++ 11
} // End of main function
2018-2019 Page 10 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3
#include <iostream>
using namespace std;
class FTest
{
public:
void dis1 ( ){ cout<<a<<" , "<<b<<endl; }
private:
int a = 2 ; int b = 4 ;
}; 1- Add the following line to the class:
void dis2 ( ){
friend void dis2( );
FTest ft;
2- Try the friend class by yourself.
ft.a =10 ;
ft.b =30;
cout<<ft.a<<" , "<<ft.b<<endl; }
int main ( )
{
FTest ft;
ft.dis1();
dis2();
return 0;
} // End of main function
Exercises
Homework
Will be taken in
the lab
2018-2019 Page 11 of 11