+ Link For Assignments, GDBs & Online Quizzes Solution |
+ Link For Past Papers, Solved MCQs, Short Notes & More |
Assignment No. 02 CS201: Introduction to Programming |
Total Marks: 20
Due Date: 27/01/2014 |
||
Instructions: Please read the following instructions carefully before submitting assignment. It should be clear that your assignment will not get any credit if:
Note: Objective: The objective of this assignment is to provide hands on experience of:
Guidelines:
|
|||
Assignment |
|
||
Problem Statement: Write a program using class(constructor, destructor, other member functions) to find the frequency of letters (small and capital alphabet or digit) from your student id character string. For example, frequency of letter ‘4’ in the string of a student id: “MC124514517” is 2.
Detailed Description: You are required to write a program which contains a class myClass having the following data members:
char*str, alphabet; and the following member functions:
The main() function should create an object of myClass and through this object member functions are called one by one. When the object is created then constructor of myClass should dynamically allocate the memory using new operator for your VUID and displays a message after storing the default id in data member character pointer str. “Default Constructor is called and default id is MC123456789” Similarly, setId() member function when called with your VUID passed as parameter, should set the value of data member character pointer str with your VUID. getId() function should return the value of data member character pointer str to the main(), then following message with your VUID should be displayed: “Your ID is MC120403765” Finally countfunction() must get input (any alphabet) from user by displaying the message, “Enter letter you want to count:” Then this function counts frequency of the specified letter from your VUID and displays it. Destructor at the end of the program should be called and display message: “Destructor called… and object has destroyed” Sample output of program: |
|||
Tags:
+ http://bit.ly/vucodes (Link for Assignments, GDBs & Online Quizzes Solution)
+ http://bit.ly/papersvu (Link for Past Papers, Solved MCQs, Short Notes & More)
+ Click Here to Search (Looking For something at vustudents.ning.com?) + Click Here To Join (Our facebook study Group)Replies are closed for this discussion.
#include<iostream>
#include<conio.h>
using namespace std;
class myClass
{
private :
char*str, alphabet;
public:
myClass() // this is Default Constructor class name and function name is same
{
cout"Default Constructor is called ..... and default id is DC130400104 \n";
}
void setId(char *id)
{
str = new char[10];// setting string array character
strcpy(str,id); // copied ID value in str varaible
}
char * getId() //ID is DC130400104 return when we call this function through object
{
return str;
}
~myClass() //this is Destructor
{
delete[]str;
cout "\n Destructor called ...... and object has destroyed \n";
cout"Press any key to continue ..... ";getch();
}
};
Malika Eman sis
sir tariq ny ooper jo solution upload kia hai us mein commenting kesi hai ap k khyal mein theek hai ya nai kindly guide me in this regard
yes dear, wo bilkul theek hai. but i think itna detail mein janay ki zaroorat nhi...just simply itna show kr diya jaye k...its Constructor, Destructor , function member, main Fucntion, Defining Class etc...
#include <cstdlib>
#include <iostream>
using namespace std;
char *str = "DC130400104";
void countfunction()
{
char alpha;
cout "Enter value for match= ";
cin >> alpha;
int i=0,count=0;
while(str[i] != '\0')
{
if (alpha == str[i])
{
count++;
}
i++;
}
cout "Your Alpha is =" alpha "Count val is = " count++;
}
main()
{
cout "DC130400104 \n";
countfunction();
system("PAUSE");
}
Assalam-u-Alaikum
kia hm iss assignment mein default ID ki jga user input leny ka method b use kr skty hein.. I mean user can enter his/her own ID to find the frequency of character???
yes, you can do it but u 'll must hv to do same like ur given output. means default id.
so, its better do not it.
#include<iostream>
#include<conio.h>
using namespace std;
class myClass
{
private :
char*str, alphabet;
public:
myClass()
{
str = new char[10];
cout"Default Constructor is called... and default id is MC \n";
}
void setId(char *id)
{
str = new char[10];
strcpy(str,id);
}
char * getId()
{
return str;
}
void countfunction()
{
cout"\nEnter letter you want to count: ";
cin>>alphabet;
int i =0, count = 0;
while(str[i] != '\0')
{
if(alphabet == str[i])
{
count++;
}
i++;
}
cout"The total letters "alphabet" in your student id : "countendl;
}
~myClass()
{
delete[]str;
cout"\nDestructor called... and object has destroyed \n";
cout"Press any key to continue . . . ";getch();
}
};
main()
{
myClass obj;
obj.setId("MC");
cout"Your id is " obj.getId();
obj.countfunction();
}
using namespace std;
#include<iostream.h>
#include<stdlib.h>
class myClass
{
private :
char*str, alphabet;
public:
myClass();
void setId(char *);
char * getId();
void countfunction();
~myClass();
};
myclass::myClass()
{
str = new char[10];
cout"Default Constructor is called ..... and default id is mc130401520";
}
myclass::void setId(char *a)
{
str = new char[10];
strcpy(str,a);
}
void myclass::getid
{
return str;
}
void myclass::countfunction()
{
cout"\nEnter letter : ";
cin>>alphabet;
int i =0, count = 0;
while(str[i] != '\0')
{
if(alphabet == str[i])
{
count++;
}
i++;
}
cout"The total letters "alphabet" in your student id : "count++endl;
}
myclass::~myClass()
{
delete[]str;
cout"\nDestructor called ...... and object has destroyed \n";
}
main()
{
myClass object;
object.setId("mc130401520");
cout"Your id is " object.getId();
object.countfunction();
}
The recommended practice for class data members is to declare them in private section (not public). You will study about it in detail in Object Oriented Programming (Encapsulation and data hiding concept).
So following approach is fine:
class myClass {
private:
char* str, alphabet;
.....
};
kindly thek sy code type kr dain mai jis ko bh c++ device ma in krti hn errors daita ha compile and run ni hota please do something.
is ma kiya error ha????????
cout"Default Constructor is called .... and default id is MC100402182 \n";
is me koi error nhi
#include<iostream.h>
#include<conio.h>
using namespace std;
class myClass
{
private :
char*str, alphabet;
public:
myClass()
{
str = new char[10];
cout"Default Constructor is called ..... and default id is bc110401299 \n";
}
void setId(char *id)
{
str = new char[10];
strcpy(str,id);
}
char * getId()
{
return str;
}
void countfunction()
{
cout"\nEnter letter you want to count: ";
cin>>alphabet;
int i =0, count = 0;
while(str[i] != '\0')
{
if(alphabet == str[i])
{
count++;
}
i++;
}
cout"The total letters "alphabet" in your student id : "countendl;
}
~myClass()
{
delete[]str;
cout"\nDestructor called ...... and object has destroyed \n";
cout"Press any key to continue ..... ";getch();
}
};
main()
{
myClass obj;
obj.setId("bc110401299");
cout"Your id is ";
obj.getId();
obj.countfunction();
}
© 2021 Created by + M.Tariq Malik.
Powered by
Promote Us | Report an Issue | Privacy Policy | Terms of Service
We are user-generated contents site. All product, videos, pictures & others contents on site don't seem to be beneath our Copyrights & belong to their respected owners & freely available on public domains. We believe in Our Policy & do according to them. If Any content is offensive in your Copyrights then please email at m.tariqmalik@gmail.com Page with copyright detail & We will happy to remove it immediately.
Management: Admins ::: Moderators
Awards Badges List | Moderators Group
All Members | Featured Members | Top Reputation Members | Angels Members | Intellectual Members | Criteria for Selection
Become a Team Member | Safety Guidelines for New | Site FAQ & Rules | Safety Matters | Online Safety | Rules For Blog Post