COMPUTER SCIENCE HOLIDAY H.W
Write down the concept of
classes & Objects given below in your H.W Notebook for revising
Concept of Class and Object in C++
Class -A class is a blue print for creating objects.
Definition:
A class is a group of objects which show a common property.
OR
A class is a collection of objects of similar type.Once a class is
defined, any number of objects can be created which belong to that class.For
example – Define a class to print the values of two numbers.
Class HelloWorld
{
int one;
int two;
public:
void
setdata()
{
cout<<”enter two numbers”;
cin>>one>>two;
}
void
putdata()
{
cout<<one<<endl<<two;
}
};
int main()
{
HelloWrold obj;
Obj.setdata();
Obj.putdata();
return
0;
}
Classes are generally declared
using the keyword class, with the following format:
class class_name
{
access_specifier_1:
member;
access_specifier_2:
member;
...
};
Object -It may be defined as
identifiable identity with some characteristics and behavior.
Syntax of creating Object:
Class_name Object_Name;
If we have class named AB then
the Object of this class can be created using above syntax as
AB Obj;
Access
Specifiers in C++
Declaration of class:
class CLASS_NAME
{
//Access Specifier
Data Members
Member Functions
};
Data members are data type
properties that describe the characteristics of a class. There may be zero or
more data members of any type in a class.
Member functions are the
set of operations that may be applied to the objects of that class. It is the
interface between the class members and object. It is always single it doesn’t
make any copy. There may be zero or more member functions in a class.
Program Access Level
that control access to members from within the program. These access levels are
private, protected or public. Depending upon the access level of a class
member , access to it is allowed or denied.
Class name that serves as a
type specifier for the class using which object of this class type can be
created.
For Example
#include<iostream.h>
#include<conio.h>
class sum // class
{
int a,b,s; //data members
public:
sum() // constructor
{
a=10;
b=20;
}
void show()
//member function
{
s=a+b;
cout<<”sum of numbers is”<<s<<endl;
}
};
int
main()
{
sum obj; //object of class
obj.show();
return
0;
}
public,
protected and private are three access
specifiers in C++.
IF YOU DON'T SPECIFY ANY ACCESS
SPECIFIER WITH A DATA MEMEBR OR WITH A MEMBER FUNCTION, THE ACCESS SPECIFIER
FOR THOSE MEMBERS WILL BE TREATED AS private BY DEFAULT.
The general form of class
definition is given below.
class class-name{
private:
Data Members/Member Functions
protected:
Data Members/Member Functions
public:
Data Members/Member Functions
};
The class body contains the
declaration of members (data and functions). There are generally three types of
member in a class: private, protected and public which are grouped under three
sections namely private: protected: and public:.
The private member can be
accessed only from within the class and public members can be accessed from
outside the class also. If no access specifier (public, protected or
private) is specified then by default, members are private.
Private variables can only be
accessed by the class
Class A
{
private:
int x;
public:
void
SetX(int n) { x = n; } //this is ok
};
int main()
{
A aobj;
aobj.x = 0; //error because x is private and
not accessible outside the class
}
Protected is similar to private but can also be
accessed by inherited classes
class A
{
protected:
int x;
public:
void SetX(int n) { x = n; } // this is ok
};
class B : public A
{
public:
void ShowX() { cout << x <<
"\n"; }
// this is ok as x is protected member
};
Public member can be accessed from outside the class
Class A
{
public:
int
x
void SetX(int
n) { x = n; } // <<< this is ok
};
int main()
{
A aobj;
aobj.x = 0;
// Ok because x is public accessible outside
the class
}
}
Solve these questions
(a) What is the
difference between Global Variable and Local Variable? 2
(b)
Write the names of the header files to
which the following belong: 1
(i) strcmp() (ii) fabs()
(c)
Rewrite the following program after
removing the syntactical errors (if any). Underline each correction. 2
#include [iostream.h]
class PAYITNOW
{
int
Charge;
PUBLIC:
void Raise(){cin>>Charge;}
void Show{cout<<Charge;}
};
void main()
{
PAYITNOW
P;
P.Raise();
Show();
}
(d) Find the output of the following program: 3
#include
<iostream.h>
struct
PLAY
{ int
Score, Bonus;};
void
Calculate(PLAY &P, int N=10)
{
P.Score++;P.Bonus+=N;
}
void
main()
{
PLAY
PL={10,15};
Calculate(PL,5);
cout<<PL.Score<<”:”<<PL.Bonus<<endl;
Calculate(PL);
cout<<PL.Score<<”:”<<PL.Bonus<<endl;
Calculate(PL,15);
cout<<PL.Score<<”:”<<PL.Bonus<<endl;
}
(e) Find the output of the following
program: 2
#include <iostream.h>
#include <ctype.h>
void Encrypt(char T[])
{
for (int
i=0;T[i]!='\0';i+=2)
if
(T[i]=='A' || T[i]=='E') T[i]='#';
else
if (islower(T[i])) T[i]=toupper(T[i]);
else
T[i]='@';
}
void main()
{
char
Text[]="SaVE EArtH";//The two words in the string Text
//are separated by single space
Encrypt(Text);
cout<<Text<<endl;
}
(f)
In the following program, if the value of
N given by the user is 15, what maximum and minimum values the program could
possibly display? 2
#include <iostream.h>
#include <stdlib.h>
void main()
{
int N,Guessme;
randomize();
cin>>N;
Guessme=random(N)+10;
cout<<Guessme<<endl;
}
(c)
Define a class TEST in C++ with following
description: 4
Private Members
a.
TestCode of type integer
b.
Description of type string
c.
NoCandidate of type integer
d.
CenterReqd (number of centers required) of
type integer
e.
A member function CALCNTR() to calculate
and return the number of centers as (NoCandidates/100+1)
Public
Members
·
A function SCHEDULE() to allow user to
enter values for TestCode, Description, NoCandidate & call function
CALCNTR() to calculate the number of Centres
·
A function DISPTEST() to allow user to
view the content of all the data members
(a) What is the
difference between Object Oriented Programming and Procedural Programming? 2
(b)
Write the names of the header files to
which the following belong: 1
(i) frexp() (ii)
isalnum()
(c)
Rewrite the following program after
removing the syntactical errors (if any). Underline each correction. 2
#include
<iostream.h>
struct
Pixels
{ int Color,Style;}
void
ShowPoint(Pixels P)
{ cout<<P.Color,P.Style<<endl;}
void main()
{
Pixels Point1=(5,3);
ShowPoint(Point1);
Pixels Point2=Point1;
Color.Point1+=2;
ShowPoint(Point2);
}
(d) Find the
output of the following program: 2
#include <iostream.h>
struct Game
{
char Magic[20];int Score;
};
void main()
{
Game M={“Tiger”,500};
char *Choice;
Choice=M.Magic;
Choice[4]=’P’;
Choice[2]=’L’;
M.Score+=50;
cout<<M.Magic<<M.Score<<endl;
Game N=M;
N.Magic[0]=’A’;N.Magic[3]=’J’;
N.Score-=120;
cout<<N.Magic<<N.Score<<endl;
}
(e)In the following program, if the value
of N given by the user is 20, what maximum and minimum values the program could
possibly display? 2
#include <iostream.h>
#include <stdlib.h>
void main()
{
int N,Guessnum;
randomize();
cin>>N;
Guessnum=random(N-10)+10;
cout<<Guessnum<<endl;
}
*****************************************************************************