We have been working very hard since 2009 to facilitate in your learning Read More. We can't keep up without your support. Donate Now.

www.bit.ly/vucodes

+ Link For Assignments, GDBs & Online Quizzes Solution

www.bit.ly/papersvu

+ Link For Past Papers, Solved MCQs, Short Notes & More

                          Assignment No. 02
                        Semester: Fall 2013

             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:

  • The assignment is submitted after due date.
  • The submitted assignment does not open or file is corrupt.
  • Assignment is copied(partial or full) from any source (websites, forums, students, etc)

Note: You have to upload only .cpp file. Assignment in any other format (extension) will not be accepted and will be awarded with zero marks. For example, if you submit code in .doc (Word document) or .txt files or .exe file, no reward will be given in any case.

Objective:

The objective of this assignment is to provide hands on experience of: 

  • Basic concepts of C/C++ classes and objects
  • Dealing with Data members and member functions
  • Initialization of objects
  • Constructor
  • Destructor
  • Dynamic memory Allocation using new operator.

Guidelines:

  • Code should be properly indented and well commented.
  • Follow C/C++ rules while writing variable names, function names etc
  • Use only dev-C++ for this assignment.

 

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:

  • myClass();
  • void setId(char *);
  • char * getId();
  • void countfunction();
  • ~myClass();

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:

 


+ 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)


Views: 6425

Attachments:

Replies are closed for this discussion.

Replies to This Discussion

it has 3 errors

i have solved these errors in 5 seconds...

#include<iostream.h>
#include<conio.h>
class myClass
{
private :
char*str, alphabet;
public:
myClass()
{
str = new char[10];
cout"Default Constructor is called ..... and default id is mc110202120 \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 : count"endl;
}
~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();

}

enjoy all newly students.

Please check my program is it ok

#include<iostream>

using namespace std;

class myClass
{
public:
void setId(char *id);
char* getId();
void countfunction();
myClass();
~myClass();

private :
char* str,alphabet;

};

myClass::myClass(void)
{
cout"Default Constructor is called ..... and default id is mc120200074 \n";
}

void myClass::setId(char* id)
{
str =new char [10];
}

char* myClass::getId()
{
return str;
}

void myClass::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::~myClass(void)
{
delete[]str;
cout"\nDestructor called ...... and object has destroyed \n";
}


main ()
{
myClass obj;
obj.setId("mc120200074");
cout"Your id is " obj.getId();
obj.countfunction();
    
return 0;    
}

idea of assignment #2

//hafiz muhammad asad israr
//BC130401097

#include<iostream>
using namespace std;

class myClass{
public:

myClass();
void setId(char * a);
char * getId();
void countfunction();
void display(void);

~myClass(); 

private:

char * str , alphabet;
};

myClass::myClass(){
str = new char[20];
str= (char*)"BC130401097";
alphabet = 'm' ;
cout"Default Constructor is called and default id is BC130401097"endl;

}

void myClass::setId(char* a){

str = a;
cout"Your ID is "strendl;
}

char * myClass::getId(void){
return str ;
}

void myClass::display(void){
cout"str :"str" , alphabet : "alphabetendl;
}

void myClass::countfunction()
{
cout"Enter 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::~myClass(){ 
cout"Destructor has been call . "endl;
delete [] str ;
}


int main()
{
myClass x;
char alpha;
char * y = const_cast<char*>("BC130401097");
x.setId(y);
x.countfunction();
system("pause");
return 1;
}


#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 "MC120403765 \n";
countfunction();
system("PAUSE");
}

If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor. This implicitly declared destructor is an inline public member of its class.

The compiler will implicitly define an implicitly declared destructor when the compiler uses the destructor to destroy an object of the destructor's class type. Suppose a class A has an implicitly declared destructor. The following is equivalent to the function the compiler would implicitly define for A:

  A::~A() { }

The compiler first implicitly defines the implicitly declared destructors of the base classes and nonstatic data members of a class A before defining the implicitly declared destructor of A

A destructor of a class A is trivial if all the following are true:

  • It is implicitly defined
  • All the direct base classes of A have trivial destructors
  • The classes of all the nonstatic data members of A have trivial destructors

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

A destructor is a member function with the same name as its class prefixed by a ~(tilde). For example:

class X 
{ public:
// Constructor for class X
X();
// Destructor for class X
~X();
};

Constructors are used to create, and can initialize, objects of their class type.

You do not specify a return type for a constructor. A return statement in the body of a constructor cannot have a return value.

constructor is a member function with the same name as its class. For example:

class X 
{ public:
X(); // constructor for class X
};

IDEA SOLUTION

#include<iostream.h>
#include<conio.h>

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();

}

RSS

Latest Activity

Kinz@ MuGhaL updated their profile
4 hours ago
Kinz@ MuGhaL joined +M.Tariq Malik's group
4 hours ago
Kinz@ MuGhaL joined +M.Tariq Malik's group
4 hours ago
Kinz@ MuGhaL joined +M.Tariq Malik's group
4 hours ago
Kinz@ MuGhaL joined +M.Tariq Malik's group
4 hours ago
Muhammad Bilal replied to Imtiaz Waqar's discussion GDB No. 1 STA630 - Research Methods in the group STA630 Research Methods
5 hours ago
☆ Tina Solangi ☆ liked иαвєєℓ αнмє∂'s profile
6 hours ago
Profile Iconmahnoor omar, Maira Qurxam, Mr.Right and 24 more joined Virtual University of Pakistan
7 hours ago
+M.Tariq Malik liked Virtual Educators Academy's discussion CS619 Final Projects (Python language) Discussions
8 hours ago
Hacker updated their profile
8 hours ago
Imtiaz Waqar added a discussion to the group STA630 Research Methods
10 hours ago
Imtiaz Waqar joined +M.Tariq Malik's group
10 hours ago
Muhammad Fawad Hassan joined +M.Tariq Malik's group
11 hours ago
Maria commented on +M.Tariq Malik's group EDU101 Foundations of Education
11 hours ago
+ Faisal + replied to shifa arshad's discussion CS605 Assignment No 02 Fall 2020 Solution / Discussion Due Date: 10-dec-2020 in the group CS605 Software Engineering-II
11 hours ago
Maria joined +M.Tariq Malik's group
11 hours ago
Maria and SafeerZaidi are now friends
11 hours ago
SafeerZaidi liked Maria's profile
11 hours ago
Tasha Malik posted a status
"Bio 201 Assignment # 2 Solution https://youtu.be/Gowjc67WZ4c"
12 hours ago
Maria liked +M.Tariq Malik's profile
13 hours ago

© 2020   Created by +M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service

.