C ++ shortcut tutorial-Chapter 11-class (end)

Source: Internet
Author: User

// -- C ++ short cut tutorial -- Chapter 11 -- class (end)
// -- Chapter 11 -- class
// -- 03/28/2006 Tues.
// -- Computer lab
// -- Liwei

 

// -- Program #7 use struct to create class
# Include <iostream>
Using namespace STD;

Struct C1 {
Int get_ I ();
Void put_ I (Int J );
PRIVATE:
Int I;
};

Int C1: get_ I ()
{
Return I;
}

Void C1: put_ I (Int J)
{
I = J;
}

Int main ()
{
C1 S;
S. put_ I (10 );
Cout <S. get_ I () <'/N ';

Return 0;
}

 

// -- Program #8 use struct to create class (this use Class)
# Include <iostream>
Using namespace STD;

Class C1 {
Int I;
Public:
Int get_ I ();
Void put_ I (Int J );
};

Int C1: get_ I ()
{
Return I;
}

Void C1: put_ I (Int J)
{
I = J;
}

Int main ()
{
C1 S;
S. put_ I (10 );
Cout <S. get_ I () <'/N ';

Return 0;
}

// -- Program #9 use Union to create class
# Include <iostream>
Using namespace STD;

Union u_type {
U_type (short int );
Void showchars ();
Short int I;
Char ch [2];
};

U_type: u_type (short int)
{
I =;
}

Void u_type: showchars ()
{
Cout <ch [0] <"" <ch [1] <'/N ';
}

Int main ()
{
U_type U (22252 );
U. showchars ();

Return 0;

}

 

 

// -- Program #10 use inline
# Include <iostream>
Using namespace STD;

Class C1 {
Int I;
Public:
Int get_ I ();
Void put_ I (Int J );
};

Inline int C1: get_ I ()
{
Return I;
}

Inline void C1: put_ I (Int J)
{
I = J;
}

Int main ()
{
C1 S;
S. put_ I (10 );
Cout <S. get_ I ();

Return 0;
}

 

 

// -- Program #11 use inline
# Include <iostream>
Using namespace STD;

Class C1 {
Int I;
Public:
Int get_ I () {return I ;};
Void put_ I (Int J) {I = J ;};
};

 

Int main ()
{
C1 S;
S. put_ I (10 );
Cout <S. get_ I ();

Return 0;
}

 

 

// -- Program #12 use object Array
# Include <iostream>
Using namespace STD;

Enum resolution {low, medium, high };

Class display {
Int width;
Int height;
Resolution Res;
Public:
Void set_dim (int w, int h) {width = W; Height = H ;};
Void get_dim (Int & W, Int & H) {W = width; H = height ;};
Void set_res (resolution R) {res = r ;}
Resolution get_res () {return res ;}
};

Char Names [3] [7] = {
"Low ",
"Medium ",
"High ",
};

Int main ()
{
Display display_mode [3];
Int I, W, h;

Display_mode [0]. set_res (low );
Display_mode [0]. set_dim (640,480 );

Display_mode [1]. set_res (medium );
Display_mode [1]. set_dim (800,600 );

Display_mode [2]. set_res (high );
Display_mode [2]. set_dim (1600,1200 );

Cout <"available display modes:/n ";

For (I = 0; I <3; I ++)
{
Cout <Names [display_mode [I]. get_res ()] <":";
Display_mode [I]. get_dim (W, H );
Cout <W <"by" <H <'/N ';
}

Return 0;
}

 

 

 

// -- Program #13 Use object Array
# Include <iostream>
Using namespace STD;

Class SAMP {
Int;
Public:
SAMP (int n) {A = N ;}
Int get_a () {return ;}
};

Int main ()
{
SAMP samparray [4] = {-1,-2,-3,-4 };
Int I;

For (I = 0; I <4; I ++)
Cout <samparray [I]. get_a ();
 
Cout <'/N ';

Return 0;

}

 

 

 

// -- Program #14 Use object Array
# Include <iostream>
Using namespace STD;

Class SAMP {
Int A, B;
Public:
SAMP (int n, int m) {A = N; B = m ;}
Int get_a () {return ;}
Int get_ B () {return B ;}
};

Int main ()
{
SAMP samparray [4] = {SAMP (1, 2), SAMP (3, 4), SAMP (5, 6), SAMP (7, 8 )};
Int I;

For (I = 0; I <4; I ++)
Cout <samparray [I]. get_a () <''<samparray [I]. get_ B () <'/N ';
 
Cout <'/N ';;

Return 0;

}

 

 

 

 

// -- Program #15 use object point
# Include <iostream>
Using namespace STD;

Class p_example {
Int num;
Public:
Void set_num (INT Val) {num = val ;}
Void show_num ();
};

Void p_example: show_num ()
{
Cout <num <'/N ';
}

Int main ()
{
P_example OB, * P;

Ob. set_num (1 );
Ob. show_num ();

P = & ob;
P-> show_num ();

Return 0;
}

 

 

 

// -- Program #16 use object point
# Include <iostream>
Using namespace STD;

Class p_example {
Int num;
Public:
Void set_num (INT Val) {num = val ;}
Void show_num ();
};

Void p_example: show_num ()
{
Cout <num <'/N ';
}

Int main ()
{
P_example ob [2], * P;

Ob [0]. set_num (10 );
Ob [1]. set_num (20 );

P = & ob [0];
P-> show_num ();

P ++;
P-> show_num ();

P --;
P-> show_num ();

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.