学校的习题

写出下面程序的运行结果。

#include<iostream>        

    using  namespace  std;

    class  example

    {
    
    

        public:

            example(int  n)

            {
    
       i=n;

                cout<<"Constructing\n";

            }

            ~example()  

            {
    
      cout  <<"Destructing\n";    

            }

            int  get_i()  

            {
    
        return  i;  

            }

        private:

            int  i;

    };

    int  sqr_it(example  o)

    {
    
       return  o.get_i()*  o.get_i();

    }

    int  main()

    {
    
       example  x(10);

        cout<<x.get_i()<<endl;

        cout<<sqr_it(x)<<endl;

        return  0;

    }

Constructing

10

100

Destructing

Destructing​

 #include<iostream>        

    using  namespace  std;

    class  aClass

    {
    
      

        public:

            aClass()

            {
    
       total++;}

            ~aClass()

            {
    
       total--;}

                int  gettotal()

                {
    
       return  total;}

        private:

            static  int  total;

    };

    int  aClass::total=0;

    int  main()

    {
    
       aClass  o1,o2,o3;

        cout<<o1.gettotal()<<"  objects  in  existence\n";

        aClass  *p;

        p=new  aClass;  

        if  (!p)

        {
    
       cout<<"Allocation  error\n";

            return  1;

        }

        cout<<o1.gettotal();

        cout<<"  objects  in  existence  after  allocation\n";

        delete  p;

        cout<<o1.gettotal();

        cout<<"  objects  in  existence  after  deletion\n";

        return  0;

    }

3 objects in existence

4 objects in existence after allocation

3 objects in existence after deletion

    #include<iostream>        

    using  namespace  std;

    class  test

    {
    
      

        public:

           test()  ;  

           ~test(){
    
      };  

        private:

           int  i;    };

    test::test()

    {
    
       i = 25;

        cout<<"Here's  the  program  output.  \n";

        cout<<"Let′s  generate  some  stuff...\n";

        for  (int  ctr=0;  ctr<10;  ctr++)

        {
    
       cout<<"Counting  at  "<<ctr<<"\n";

        }

    }

    test  anObject;

    int  main()

    {
    
       return  0;

    }

Here's the program output.

Let′s generate some stuff…

​Counting at 0

Counting at 1

Counting at 2

Counting at 3

Counting at 4

Counting at 5

Counting at 6

Counting at 7

Counting at 8

Counting at 9

 #include<iostream>        

    using  namespace  std;

    class  R{
    
    

        public:

            R(int  r1,int  r2)

            {
    
       R1=r1;

                R2=r2;

            }

            void  print();  

            void  print()    const;

        private:

            int  R1,R2;

    };

    void  R::print()

    {
    
      cout<<R1<<","<<R2<<endl;

    }

    void  R::print()  const

    {
    
       cout<<R1<<","<<R2<<endl;

    }

    int  main()

    {
    
       R  a(6,8);

        const  R  b(56,88);

        b.print();

        return  0;

    }

56,88

  #include<iostream>        

    using  namespace  std;

    class  Sample{
    
    

        public:

            Sample(  int  i,int  j)

           {
    
       x=i;

               y=j;

            }

            void  disp()  

            {
    
       cout<<"disp1"<<endl;

            }

            void  disp()  const

            {
    
       cout<<"disp2"<<endl;

            }

        private:

            int  x,y;

    };

    int  main()

    {
    
       const  Sample  a(1,2);

        a.disp();

        return  0;

    }

disp2

    #include<iostream>        

    using  namespace  std;

    class  A{
    
    

        public:

            void    set(int  i,int  j)

            {
    
       x=i;

                y=j;

            }

            int    get_y()

            {
    
       return  y;

            }

        private:

            int  x,y;

    };

    class  box{
    
    

        private:

            int  length,width;

            A  label;

        public:

            void  set(int  l,int  w,int  s,int  p)

            {
    
       length=l;

                width=w;

                label.set(s,p);

            }

            int  get_area()

            {
    
       return  length*width;

            }

    };

    int  main()

    {
    
       box  b;

        b.set(4,6,1,20);

        cout<<b.get_area()<<endl;

        return  0;

    }

24

#include<iostream>        

    using  namespace  std;

    class  B  {
    
    

        public:

            B(){
    
    }

            B(int  i,int  j)

            {
    
       x=i;

                y=j;

            }

            void  printb()

            {
    
      cout<<x<<","<<y<<endl;

            }

        private:

            int  x,y;

    };

    class  A{
    
    

        public:

            A()

            {
    
      }

            A(int  i,int  j);

            void  printa();

        private:

            B  c;

    };

    A::A(int  i,int  j):c(i,j)

    {
    
      }

    void  A::printa()

    {
    
       c.printb();

    }

    int  main()

    {
    
       A  a(7,8);

        a.printa();

        return  0;

    }

7,8


#include <iostream>

using namespace std;

class B1{
    
    

 public:

B1(int i)

{
    
     b1=i;cout<<"Constructor B1. "<<endl; }

void Print()

{
    
     cout<<b1<<endl;}

private:

int b1;

};

class B2 {
    
    

public:

B2 (int i)

{
    
     b2=i; cout<<"Constructor B2. "<<endl;}

void Print()

{
    
     cout<<b2<<endl;}

private:

int b2;

};

class A: public B2, public B1

{
    
    

public:

A(int i,int j,int l);

void  Print();

private:

int a;

};

A::A(int i,int j,int l):B1(i), B2(j)

{
    
     a=l; cout<<"Constructor A. "<<endl;}

void A:: Print()

{
    
    B1:: Print();

B2:: Print();

cout<<a<<endl;

}

int main()

{
    
    A aa(3,2,1);

aa. Print();

return 0;

}

Constructor B2.

Constructor B1.

Constructor A.

3

2

1

#include<iostream>

using namespace std;

class A{
    
    

private:

int a;

public:

A()

{
    
    a=0;}

A(int i)

{
    
    a=1;}

void Print()

{
    
    cout<<a<<",";}

};

class B: public A

{
    
    

private:

int b1, b2;

public:

B()

{
    
    b1=0;b2=0;}

B(int i)

{
    
    b1=i;b2=0;}

B(int i, int j,int k): A(i), b1(j),b2(k)

{
    
    }

void Print()

{
    
    A:: Print();

cout<<b1<<","<<b2<<endl;

}

};

int main()

{
    
     B ob1,ob2(1),ob3(3,6,9);

ob1.Print();

ob2.Print();

ob3.Print();

return 0;

}


0,0,0
0,1,0
1,6,9


#include <iostream>
using namespace std;
class shapes {
    
    
	protected:
		int x, y;
	public:
		void setvalue(int d, int w = 0) {
    
    
			x = d;
			y = w;
		}
		virtual void disp() = 0;

};

class square: public shapes {
    
    
	public:
		void disp() {
    
    
			cout << x *y << endl;
		}
};

int main() {
    
    
	shapes *ptr;
	square s1;
	ptr = &s1;
	ptr->setvalue(10, 5);
	ptr->disp();
	return 0;
}

50

#include <iostream>
using namespace std;

class Stock {
    
    
	public:
		virtual void print() {
    
    
			cout << "Stock class.\n";
		}
};

class Der1_Stock: public Stock {
    
    
	public:
		void print() {
    
    
			cout << "Der1_Stock class.\n";
		}
};

class Der2_Stock: public Stock {
    
    
	public:
		void print() {
    
    
			cout << "Der2_Stock class.\n";
		}
};

int main() {
    
    
	Stock s1;
	Stock *ptr;
	Der1_Stock d1;
	Der2_Stock d2;
	ptr = &s1;
	ptr->print();
	ptr = &d1;
	ptr->print();
	ptr = &d2;
	ptr->print();
	return 0;
};

Stock class.
Der1_Stock class.
Der2_Stock class


 #include <iostream>
using namespace std ;
class Array{
    
    
   public :
      Array(int);
      int& operator ()(int);                 //重载运算符()
   private :
      int *m ;
      int x ;
} ;
Array :: Array(int x)
{
    
      this->x=x ;
  m=new int[x] ;
  for(int i=0;i<x;i++)
     *(m+i)=i;
}
int& Array :: operator() (int x1)
{
    
      return(*(m+x1)) ; }
int main ()
{
    
      Array a(10) ;
   cout<<a(5) ;
   a(5) =7 ;
   cout<<a(5) ;
return 0 ;
}

57

#include <iostream>

using namespace std;

class A {
    
    

	public :

		A(int i) : x(i)

		{
    
      }

		A ()

		{
    
    
			x = 0 ;
		}

		friend A operator++(A a) ;

		friend A operator--(A &a) ;

		void print () ;

	private :

		int x ;

} ;

A operator++(A a)

{
    
    
	++a.x;

	return a ;

}

A operator--(A &a)

{
    
    
	--a.x ;

	return a;

}

void A::print()

{
    
    
	cout << x << endl;
}

int main()

{
    
    
	A a(7) ;

	++a;

	a.print() ;

	--a ;

	a.print() ;

	return 0 ;

}

7
6

猜你喜欢

转载自blog.csdn.net/weixin_52045928/article/details/118098219