Sunday 1 January 2012

XII. COMPUTER ETHICS


XII. COMPUTER ETHICS


SHORT ANSWERS

1.What are ethics?
  • Generally speaking, ethics is the set of rules for determining moral standards or socially acceptable behaviours.
2.What are the general guidelines on computer ethics?
The general guidelines on computer ethics are:
1.Protection of personal data,
2.Protection from computer crime and
3.Projection from cracking.
3.What are the three important methods of protecting personal data?
The three important methods of protecting personal data are as follows:
(i)Physical security,
(ii)Personal security and
(iii)Personnel security.
4What does physical security refer to?
  • Physical security refers to the protection of hardware, facilities, magnetic disks, and other items that could be illegally accessed, stolen, damaged or destroyed.
  • This is usually provided by restricting the people who can access the resources.
5.What does personal security refers to?
  • Personal security refers to software setups that permit only authorized access to the system.
  • User IDs and passwords are common tools for such purpose.
6.What does personnel security refers to?
Personnel security refers to protecting data and computer system against dishonesty or negligence of employees.
7.What is a computer crime?
A computer crime is any illegal activity using computer software, data or access as the object, subject or instrument of the crime.
8.List out some of the common computer crimes.
Some of the common computer crimes are:
  • Crimes related to money transfer on the internet.
  • Making long distance calls illegally using computers.
  • Illegal access to confidential files.
  • Stealing hardware.
  • Selling or misusing personal.
  • Hardware and software piracy.
  • Virus
  • Cracking
  • Theft of computer time.
9.What is piracy?
Making and using duplicate hardware and software is called piracy.
10.Why do we generally tend to pirate?
  • We like free things.
  • Why pay for something when we can get it for free?
  • Our thinking and actions are self-serving.


11.What is a computer virus?
  • A virus is a self-replicating program that can cause damage to data and files stored on the computer.
  • These are programs written by programmers with great programming skills
  • 57000 known virus programs are in existence
  • 6 new virus are found each day
12.What is called theft of “Computer time”?
  • When computers in an organisation are idle, some other software may run on these idle systems without the knowledge of the organization is called theft of “Computer time”.
13.List out any two of ten commandments of computer ethics written by Computer Ethics Institute.
  • Should not use a computer to harm other people.
  • Should not interfere with other people’s computer work.
  • Should not snoop around in other people’s computer files.
14.What is cracking? What are the reasons for committing this crime?
  • Cracking is the illegal access to the network or computer system.
  • Illegal use of special resources like hardware, software, files in the key reason for cracking.
  • Revenge, business reasons and thrill are other common reasons for committing this crime. 

VIII. CONSTRUCTORS AND DESTRUCTORS



VIII. CONSTRUCTORS AND DESTRUCTORS


SHORT ANSWERS

1.What is a constructor?
  • The constructor function initializes the class object.
  • The memory space is allocated to an object.
  • A constructor is automatically invoked when an object is created.
SYNTAX
Class simple
{
……..
}
public:
simple()
};
2.List out the Rules for constructor definition and usage:
  • The name of the constructor must be same as that of the class
  • A constructor can have parameter list
  • The constructor function can be overloaded
  • The compiler generates a constructor, in the absence of a user defined constructor
  • The constructor is executed automatically
  • They are declared under public:
3.What are the functions of a constructor?
Functions of constructor:
  • The constructor function initializes the class object.
  • The memory space is allocated to an object.
4.What are the types of constructor?
1.Default constructor (or) Non- Parameterized constructor
2.Parameterized constructor
3.Copy constructor
4..Match the following for the class my class.
1.myclass ( ) a.copy constructor
2.myclass (int x, int y) b.method
3.void show ( ) c.default constructor
4.myclass (myclass & a) d.parameterized constructor
Ans:1. (c)2. (d) 3. (b) 4. (a)
5.What is Non-Parameterized (or) default constructor
  • It is a constructor without parameters
  • It is called as default constructor
  • It is referred to compiler generated constructors
  • It is executed when an object without parameters is declared
  • ex. add()
6.What is Parameterized constructor?
  • It is a constructor with parameters
  • To invoke this constructor , the object should be declared with integer constants or variables
  • Ex. add(int a,int b)
7.When does a copy constructor get executed?
A copy constructor is executed
1.When an object is passed as a parameter to any of the member function.
Example add c(b);
2.When a member function returns an object.
Example a.add(b);
3.When an object is passed by reference to constructor.
Example, add a , b(a);
8.Explain constructor overloading.
Function overloading can be applied for constructors, as constructors are special functions of classes
Three types of constructors,they are
1.Non-Parameterized constructor
2.Parameterized constructor
3.copy constructor
9..What is a destructor?
  • A destructor is a function that removes the memory of an object
  • It is executed when the scope of the object is exit.
  • It carries the same name as the class tag, but with a tilde (~) as prefix.
SYNTAX
Class simple
{
……..
}
public:
~simple()
};

10.Write the rules of destructor?
  • The destructor has the same name as that of the class prefixed by the tilde character ‘~’.
  • The destructor cannot have arguments
  • It has no return type
  • Destructors cannot be overloaded
  • There can be only one destructor in a class
  • In the absence of user defined destructor, it is generated by the compiler
  • It is executed when the scope of the object is exit.

11.What is constructor overloading?
Function overloading can be applied for constructors, as constructors are special functions of classes.
Constructor overloading includes parameterized (default) constructor, Non-Parameterized constructors and copy constructor
12. What are the rules for constructor definition and usage
13. What are the rules for destructor definition and usage.
14.What are the similarities of Constructors and Destructor?
  • The Constructors and Destructor have the same name as that of the class
  • They are not associated with any data type.
  • Both the functions return nothing
  • Both are generated by the complier in the absence of User Defined Constructors/ Destructor respectively.
  • Both are executed automatically.
  • Both are declared under public:


15.Differentiate between Constructor and Destructor.

Constructors
Destructor
The name of the constructor must be same as that of the class

The destructor has the same name as that of the class prefixed by the tilde character ‘~’.

The constructor is executed automatically

The Destructor is executed automatically

can have parameter list
cannot have parameter
no return type
no return type
can be overloaded
cannot be overloaded, only one destructor in a class
It is executed when the scope of the object is created.

It is executed when the scope of the object is exit.


Additional questions
1)Find the out out of the following program
#include <iostream.h>
#include <conio.h>
class add
{
int num1, num2, sum;
public:
add ( )
{
cout<< "\n constructor without parameters..";
num1=0;
num2=0;
sum=0;
}
add (int s1, int s2)
{
cout<< "\n parameterized constructor..";
num1 = s1;
num2 = s2;
sum = NULL;
}
add (add & a)
{
cout<< "\n Copy constructor…";
num1 = a.num1;
num2 = a.num2;
sum = NULL;
}
void getdata ( )
{
cout<< "Enter a data";
cin>> num1>>num2;
}
void addition ( )
{
sum=num1+num2;
}
void putdata ( )
{
cout<< "\n The numbers are..";
cout<<num1<< "\t" <<num2;
cout<< "\n The sum of the numbers are .."<<sum;
}
};
void main ( )
{
clrscr();
{
clrscr ( );
add a, b (10, 20), c(b);
a. getdata ( );
a. addition ();
b. addition ( );
c. addition ( );
cout<< "\n object a:";
a. putdata ( );
cout<< "\n object b:";
b. putdata ( );
cout<< "\n object c:<<";
c. putdata ( );
}

getch();
}

OUTPUT:
Constructor without parameters….
Parameterized Constructor...
Copy Constructors…
Enter data .. 5 6
Object a:
The numbers are 5 6
The sum of the numbers are ….. 11
Object b:
The numbers are 10 20
The sum of the numbers are … 30
Object c:
The numbers are 10 20
The sum of the numbers are ….. 30
2)Find the out out of the following program
#include<iostream.h>
#include<conio.h>
class simple
{
private:
int a,b;
public:
simple()
{
a= 0 ;
b= 0;
cout<< “\n Constructor of class-simple “;
}
~simple()
{
cout<<“\n Destructor of class – simple .. “;
}
void getdata()
{
cout<<“\n Enter values for a and b... “;
cin>>a>>b;
}
void putdata()
{
cout<<“\nThe two integers .. “<<a<<‘\t’<< b;
cout<<“\n The sum of the variables .. “<< a+b;
}
};
void main()
{
simple s;
s.getdata();
s.putdata();
getch();}

output:
Constructor of class - simple ..
Enter values for a & b… 5 6
The two integers….. 5 6
The sum of the variables….. 11
Destructor of class - simple …


III.Find the out out of the following program

#include <iostream.h>
            #include <conio.h>
            class distance
            {
                    int feet;
                    float inch;
            public:
                    distance ( )
                              {
                              feet = 0;
                              inch = 0;
                              }
                    distance (int x, float y)
                              {
                              feet = x;
                              inch = y;
                              }
                    distance (float a)
                              {
                              feet = int (a);
                              inch = (a-feet)* 12;
                              }
                    void putdata ( )
                              {            
                              cout<< “\n Distance is “<<feet<<” feet and “<<inch<<” inches”;
                    };
                    void main ( )
                    {
                    clrscr ( );
                    distance d1;
                    distance d2 (4, 5), d3 (5, 3.6);
                    d1.putdata ( );
                    d2.putdata ( );
                    d3.putdata ( );
                    getch ( );
                    }
output:


Distance is 0 feet and 0 inches                                 
Distance is 4 feet and 5 inches
Distance is 5 feet and 3.6 inches

IV.Find the out out of the following program
#include <iostream.h>
            #include <conio.h>
            class cartesian
            {
                              int x, y;
            public:
            cartesian (int a, int b)
                    {
                              x = a;
                              y = b;
                              }
            cartesian ( )
                    {
                              x = 0;
                              y = 0;
                              }
            void print ( )
                    {
                              cout<< “\n (“<<x<<”, “<<y<<”)”;
                              }
            };    
            void main ( )
            {
            clrscr ( );
            cartesian p1, p2 (7, -1), p3 (4, 5);
            p1.print ( );
            p2.print ( );
            p3.print ( );
            getch ( );
           
            Output:
                0, 0
            7, -1
            4, 5        

V.Read the following program and answer the following question.           
            #include <iostream.h>
            #include <conio.h>
            class simple
            {      float x;
                    public:
                    simple ( )
                    {
                    x = 1.0;
                    }
                    simple (float m)
                    {
                    x = m;
                    }
                    simple (float a, float b)
                    {
                    x = a + b;
                    }
                    simple (simple & k)
                    {
                    x = k.x;
                    cout<< “\n copy constructor invoked \n”;
                    }
                    void show ( )
                    {
                    cout<< “\n x =” <<x<< ‘\n’;
                    }
            };
            void main ( )
            {      clrscr ( );
                    simple s, s1;
                    s1. show ( );
                    simple s2 (7), s3 (s1);
                    simple s4 = s2,  s5 (4, 5);
                    s = s5;
                    s2.show ( );
                    s3.show ( );
                    s4.show ( );
                    s5.show ( );
                    s.show ( );
                    getch ( );
            }
            (a)   Write prototype of non-parameterized constructor.
                    simple ( );
            (b)   Write prototype of parameterized constructor.
                    simple (float m);
                    simple (float a, float b)
            (c)    Write prototype of copy constructor.
                    simple (simple & k);
            (d)   Name the private members of the class.
                    float x
            (e)    Name the public members of the class.
                    void show ( );
            (f)    Identify statements that invoke copy constructor.
                    s4 = s2
                    s = s5;
VI.Read the following program and answer the following question.

            #include <iostream.h>
            #include <conio.h>
            class example
            {
                    int x, y;
            public:
                    example (example & a)
                    {
                              x = a.x;
                              y = a.y;
                              cout << “copy constructor …”;
                    }
                    example ( )
                    {
                              x = 1;
                              y = 1;
                    }
                    example (int a, int b)
                    {
                              x = a;
                              y = b;
                    }
                    void display ( )
                    {
                              cout<< “\nx =” <<x<< “and y =” <<y;
                    }
            };
            void main ( )
            {
            clrscr ( );
            example e1;
            example e2 (2, 4);
            example e3 = e2;
            e1.display ( );
            e2.display ( );
            e3.display ( );
            getch( );
            }
            (a)   Write prototype of non parameterized constructor.
                    example ( );
            (b)   Write prototype of parameterized constructor.
                    example (int a, int b);                                       
            (c)   Write prototype of copy constructor.
                    example (example & a);                          
            (d)   Name private members of the class.
                    int x, y.
            (e)   Name public members of the class.
                    void display ( );
            (f)    Indentify statements that invoke copy constructor.
                    example e3 = e2;




VI.Debug the following program to get given output.
           
            #include <iostream.h>
            #include <conio.h>
            class class1
                    {
                    int x;
                    int y;
            public:
                    class1 ( )
                    {
                    x = 0;
                    y = 0;
                    }
                    class1 (int m, int n)
                    {
                    m = x;
                    n = y;
                    }
                    void print ( )
                    {        
                    cout<<endl<<x<< ‘\t’ <<y;
                    }
            };
            void main ( )
            {
            class1 c1, c2 (3, 4);
            c1.print ( );
            c2.print ( );
            }

Output:
                   0     0
                  3     4
Solution:
            (i)    m = x;
                    error:  the statement must be given as x = m;
            (ii)   n = y;
                    error:  the statement must be given as y = n;
         


















Book Exercises:
I)
1.Complete the following table


Constructor
Destructor
1.Should be declared under
Public
Public
2.Overloading is  
allowed
Not allowed
3.Is executed when an object is
Created
Removed
4.The function of a
Initialize and allocate memory space to an object  
Remove allocated memory of an object



II. Why do the following snippets throw error?
a)
Answer: Constructor must be declared under public scope.
class simple
{
private :
int x;
public:
simple ( )
{ x = 5; }
} ;
b)
Simple s ; Error
Answer: Cannot create object s as it does not match any constructor type.
class simple
{
private :
int x;
public:
simple (int y)
{ x = y; }
} ;
void main ( )
{
simple s(15);
}
c)
  • The Two prototypes simple (int y); simple (int z = 5);
are similar.
  • They do not differ either by the type of parameter or number of parameters.
  • Therefore compiler throws an error